473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

graphics in c

Raj
following is a code for scan converting a line using DDA algorithm.
But I'm not getting any line in the output. The code is:
/*start*/
#include <graphics.h>
#include <stdio.h>
float m;
main()
{
int x1,y1,x2,y2;
float dx,dy;
void slopelessthanone(int,int,int);
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
printf("Enter coordinates of first point:");
scanf("%d%d",&x1,&y1);
printf("Enter coordinates of second point:");
scanf("%d%d",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
m=dy/dx;
printf("%f",m);
if (abs(m)<1) /*I'm considering only one case initially*/
slopelessthanone(x1,x2,y1);
getch();
closegraph();
return;
}
void slopelessthanone(int x1, int y1, int x2)
{
float x,y;
int i=1;
x=x1;
y=y1;
clrscr();
while(x<=x2){
putpixel(x,y,WHITE);
x+=1;
y+=i*m;
i++;
}
}
/*end

Basically in the output when i enter coordinates of the two points,
only one dot(i.e. one pixel) gets plotted. There is no line segment. I
guess this means that the while loop is executing only once. But i
couldn't find the reason.
If anybody could help... thanx!

Aug 12 '07 #1
11 2274
On 2007-08-12, Raj <za*****@gmail.comwrote:
float m;
[...]
void slopelessthanone(int x1, int y1, int x2)
{
float x,y;
int i=1;
x=x1;
y=y1;
clrscr();
while(x<=x2){
putpixel(x,y,WHITE);
x+=1;
y+=i*m;
i++;
}
}
The intention here is to draw points on a line segment
with slope m that starts at (x1,y1) and extends to x = x2.

In each iteration x is incremented by 1. The corresponding y
should be incremented by m. Therefore you should say y+=m,
not y+=i*m.

--
Rouben Rostamian
Aug 12 '07 #2
On Sun, 12 Aug 2007 10:32:41 -0700, Raj wrote:
On Aug 12, 9:46 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
>Raj <zape...@gmail.comwrites:
Well yes... i also figured out the 0.5 addition from someone else. And
i guess it would be fine enough if i add anly y+m and not use i at
all. And i AM using x2 as my 3rd parameter. Check my earlier post :)
Well my new while loop is:
while(x<=x2){
x+=1;
y+=m;
putpixel(x,y+0.5,WHITE);
}
But it's still not working! :(
& i still dont understand why i should use y as double.

This will get confusing if you post fragments. Is y double in the
above? It looks OK (as far as one can tell from a fragment of code)
if y is a double, and it looks like it won't work at all if y is
declared int.
[snip]
I just meant that i've taken y as a float & not double... still no
output...
This can't be the cause of your problem.
Try replacing putpixel(x, y+0.5, WHITE) with
printf("%d %d\n", (int)x, (int)(y + 0.5)); and examine the output.

Anyway, usually there is no point in using float instead of
double, as floats will be automatically converted to doubles in
operations, so they are likely to be slower. Use float only when
saving space is vital (e.g. you have an array with one thousand
of them).
--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower

Aug 13 '07 #3
Raj
Ok here's my new (& i guess improved! :)) code.... but i guess there's
some problem in the sense that all kinds of lines aren't getting
plotted properly (at least that's what i think)... check it out plz...

/*start*/
#include <graphics.h>
#include <stdio.h>
void drawaxes();
void slopelessthanone(double,double,double,double);
void slopegreaterthanone(double,double,double,double);
main()
{
int gdriver = DETECT, gmode, errorcode;
int originx,originy;
double x1,y1,x2,y2,dx,dy,m;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
originx=getmaxx()/2;
originy=getmaxy()/2;
printf("Enter coordinates of first point:");
scanf("%lf%lf",&x1,&y1);
printf("Enter coordinates of second point:");
scanf("%lf%lf",&x2,&y2);
if (x1>=0) /*rounding off*/
x1+=0.5;
else
x1-=0.5;
if (y1>=0)
y1+=0.5;
else
y1-=0.5;
if (x2>=0)
x2+=0.5;
else
x2-=0.5;
if (y2>=0)
y2+=0.5;
else
y2-=0.5;
dx=x2-x1;
dy=y2-y1;
m=dy/dx;
x1+=originx; /*shifting the coordinate system*/
y1+=originy;
x2+=originx;
y2+=originy;
if (abs(m)<1)
slopelessthanone(x1,y1,x2,m);
if (abs(m)>=1)
slopegreaterthanone(y1,y2,x1,m);
getch();
closegraph();
return;
}
void drawaxes() /*drawing coordinate axes*/
{
line(getmaxx()/2, 0, getmaxx()/2, getmaxy());
line(0, getmaxy()/2, getmaxx(), getmaxy()/2);
}
void slopelessthanone(double x1, double y1, double x2, double m)
{
double x,y;
x=x1;
y=y1;
clrscr();
drawaxes();
if (x1<x2)
while(x<=x2){
if (y>=0)
putpixel(x,y+0.5,WHITE);
else
putpixel(x,y-0.5,WHITE);
x+=1;
y+=m;
}
else
while(x>=x2){
if (y>=0)
putpixel(x,y+0.5,WHITE);
else
putpixel(x,y-0.5,WHITE);
x-=1;
y-=m;
}
}
void slopegreaterthanone(double y1, double y2, double x1, double m)
{
double x,y;
y=y1;
x=x1;
clrscr();
drawaxes();
if (y1<y2)
while(y<=y2){
if (x>=0)
putpixel(x+0.5,y,WHITE);
else
putpixel(x-0.5,y,WHITE);
y+=1;
x+=m;
}
else
while(y>=y2){
if (x>=0)
putpixel(x+0.5,y,WHITE);
else
putpixel(x-0.5,y,WHITE);
y-=1;
x-=m;
}
}

I've devoted a lot of time to this... & honestly i don't know even if
there is a mistake here... but certain lines are going way off from
what they should look like...

Aug 13 '07 #4
On Aug 13, 4:13 pm, Raj <zape...@gmail.comwrote:
printf("Enter coordinates of first point:");
scanf("%lf%lf",&x1,&y1);
printf("Enter coordinates of second point:");
scanf("%lf%lf",&x2,&y2);
What inputs is your program receiving?

I recall issues with scanf picking up newlines and other characters
that you may not want from the buffer, as they say garbage in garbage
out :)

Aug 13 '07 #5
Raj
On Aug 13, 7:24 pm, "Very.Little.Gravitas.Indeed"
<Very.Little.Gravitas.Ind...@gmail.comwrote:
On Aug 13, 4:13 pm, Raj <zape...@gmail.comwrote:
printf("Enter coordinates of first point:");
scanf("%lf%lf",&x1,&y1);
printf("Enter coordinates of second point:");
scanf("%lf%lf",&x2,&y2);

What inputs is your program receiving?

I recall issues with scanf picking up newlines and other characters
that you may not want from the buffer, as they say garbage in garbage
out :)
Inputs are the coordinates... can be integers or real values....
fflush(stdin) doesn't help in any way... i've tried it...

Aug 13 '07 #6
On Aug 13, 4:32 pm, Raj <zape...@gmail.comwrote:
>
Inputs are the coordinates... can be integers or real values....
fflush(stdin) doesn't help in any way... i've tried it...
If the data input you are recieving in the program is correct then the
logic must be faulty

Produce a set of data and identify the data that gives the wrong
output.

Then identify the calculations your program goes through with that
data to identify the problem.

Aug 13 '07 #7
Raj wrote:
On Aug 13, 7:24 pm, "Very.Little.Gravitas.Indeed"
<Very.Little.Gravitas.Ind...@gmail.comwrote:
>On Aug 13, 4:13 pm, Raj <zape...@gmail.comwrote:
printf("Enter coordinates of first point:");
scanf("%lf%lf",&x1,&y1);
printf("Enter coordinates of second point:");
scanf("%lf%lf",&x2,&y2);

What inputs is your program receiving?

I recall issues with scanf picking up newlines and other characters
that you may not want from the buffer, as they say garbage in garbage
out :)

Inputs are the coordinates... can be integers or real values....
fflush(stdin) doesn't help in any way... i've tried it...
Of course not -- the action of `fflush` on an /input/ stream is
undefined.

At the very very least, if you're going to wrap the piano wire of
`scanf` round your wrists, have available the needles, thread, and
bandages of echoing your input when you've read it. It's very
enlightening when your input looks like two perfectly reasonable
floating-point numbers and the output looks like the diameter of
the Earth in angstroms. Or perhaps parsecs.

Also, check the return value of `scanf`.

Things are significantly more manageable if you read the entire input
line first (with `fgets` or Some Similar Utility Function) and
then read your numbers out of /that/ using `sscanf`. At least if
this gose worng you're not left with the input stream in An
Interesting Condition.

--
Chris "think Alien" Dollin

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Aug 13 '07 #8
On Aug 12, 4:53 am, Raj <zape...@gmail.comwrote:
following is a code for scan converting a line using DDA algorithm.
But I'm not getting any line in the output. The code is:
/*start*/
#include <graphics.h>
#include <stdio.h>
float m;
main()
{
int x1,y1,x2,y2;
float dx,dy;
void slopelessthanone(int,int,int);
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
printf("Enter coordinates of first point:");
scanf("%d%d",&x1,&y1);
printf("Enter coordinates of second point:");
scanf("%d%d",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
m=dy/dx;
printf("%f",m);
if (abs(m)<1) /*I'm considering only one case initially*/
slopelessthanone(x1,x2,y1);
getch();
closegraph();
return;}

void slopelessthanone(int x1, int y1, int x2)
{
float x,y;
int i=1;
x=x1;
y=y1;
clrscr();
while(x<=x2){
putpixel(x,y,WHITE);
x+=1;
y+=i*m;
i++;
}}

/*end

Basically in the output when i enter coordinates of the two points,
only one dot(i.e. one pixel) gets plotted. There is no line segment. I
guess this means that the while loop is executing only once. But i
couldn't find the reason.
If anybody could help... thanx!
This is from Graphics Gems:
void digline(int x1, int y1, int x2, int y2, void (*dotproc)
(int,int))
{
int d,
x,
y,
ax,
ay,
sx,
sy,
dx,
dy;

dx = x2 - x1;
ax = (((dx)<0) ? -(dx) : (dx)) << 1;
sx = (((dx)<0) ? -1 : 1);
dy = y2 - y1;
ay = (((dy)<0) ? -(dy) : (dy)) << 1;
sy = (((dy)<0) ? -1 : 1);

x = x1;
y = y1;
if (ax ay) {
d = ay - (ax >1);
for (;;) {
(*dotproc) (x, y);
if (x == x2)
return;
if (d >= 0) {
y += sy;
d -= ax;
}
x += sx;
d += ay;
}
} else {
d = ax - (ay >1);
for (;;) {
(*dotproc) (x, y);
if (y == y2)
return;
if (d >= 0) {
x += sx;
d -= ay;
}
y += sy;
d += ax;
}
}
}

Aug 13 '07 #9
P.S.
Posts about how to do graphics (in any language) are better targeted
to news:comp.graphics.algorithms
Aug 13 '07 #10
On Mon, 13 Aug 2007 07:13:18 -0700, Raj wrote:
Ok here's my new (& i guess improved! :)) code.... but i guess there's
some problem in the sense that all kinds of lines aren't getting
plotted properly (at least that's what i think)... check it out plz...
[snip]
void slopegreaterthanone(double y1, double y2, double x1, double m)
{
double x,y;
y=y1;
x=x1;
clrscr();
drawaxes();
if (y1<y2)
while(y<=y2){
if (x>=0)
putpixel(x+0.5,y,WHITE);
else
putpixel(x-0.5,y,WHITE);
y+=1;
x+=m;
}
else
while(y>=y2){
if (x>=0)
putpixel(x+0.5,y,WHITE);
else
putpixel(x-0.5,y,WHITE);
y-=1;
x-=m;
}
}

I've devoted a lot of time to this... & honestly i don't know even if
there is a mistake here... but certain lines are going way off from
what they should look like...
As I suggested before, try replacing putpixel() calls with
printf() calls, this should make it easier to figure out what's
wrong.

--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower

Aug 14 '07 #11
Raj wrote:
>
following is a code for scan converting a line using DDA algorithm.
But I'm not getting any line in the output. The code is:
/*start*/
#include <graphics.h>
#include <stdio.h>
float m;
main() {
int x1,y1,x2,y2;
float dx,dy;
void slopelessthanone(int,int,int);

initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
printf("Enter coordinates of first point:");
scanf("%d%d",&x1,&y1);
printf("Enter coordinates of second point:");
scanf("%d%d",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
m=dy/dx;
printf("%f",m);
if (abs(m)<1) /*I'm considering only one case initially*/
slopelessthanone(x1,x2,y1);
getch();
closegraph();
return;
}
void slopelessthanone(int x1, int y1, int x2) {
float x,y;
int i=1;

x=x1;
y=y1;
clrscr();
while(x<=x2){
putpixel(x,y,WHITE);
x+=1;
y+=i*m;
i++;
}
}
/*end

Basically in the output when i enter coordinates of the two points,
only one dot(i.e. one pixel) gets plotted. There is no line segment. I
guess this means that the while loop is executing only once. But i
couldn't find the reason.
If anybody could help... thanx!
Your question is unanswerable here, since you are using
non-standard things. From our viewpoint:

Undefined: graphics.h, initgraph, getch, closegraph, clrscr,
putpixel.

main is an int function. Return one.

slopelessthanone should precede main, so defined when called.

c.l.c discusses the standard C language, not system specific
variations.

In general, when posting code, try to use adequate blanks. There
are no prizes for suppressing blanks in source code.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 14 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: JBiagio | last post by:
Hello All, I am attempting to learn a bit about the GDI+ transforms and charting data and I feel like I'm getting a handle on how the transforms work. My chart object has a large "canvas" bitmap...
12
by: Sanjay | last post by:
hi, We are currently porting our project from VB6 to VB .NET. Earlier we used to make scale transformations on objects like pictureBox , forms etc.Now Such transformations are made on the...
2
by: John Bailo | last post by:
I am walking through some of the very first sample code from the book "Beginning .NET Game Programming" from Apress. I identify his sample code with //SC This code puzzles me: Graphics graph...
14
by: Pmb | last post by:
At the moment I'm using Borland's C++ (http://www.borland.com/products/downloads/download_cbuilder.html#) I want to be able to take an array of points and plot them on the screen. Is there a way...
5
by: Charles A. Lackman | last post by:
Hello, I have created a complete PrintDocument and need to create an image from it. How is this done? e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality...
6
by: Chris Dunaway | last post by:
The method for printing documents in .Net can be confusing, especially for newer users. I would like to create a way to simplify this process. My idea would be implemented using a PrintDocument...
12
by: Xah Lee | last post by:
Of Interest: Introduction to 3D Graphics Programing http://xahlee.org/3d/index.html Currently, this introduction introduces you to the graphics format of Mathematica, and two Java Applet...
6
by: active | last post by:
I have an image and a graphics object created (FromImage) from that image. I need to create a new image and create a new graphics object from the new image. I want the new graphics object have...
9
by: DaveL | last post by:
hello I have a Bit map 1367 wide 32 high this bitmap contains like 40 separate Images 32x32 I tell it the id*32 to get the approiate Image from the source Bitmap When i CreateGraphics()...
8
by: Abhiraj Chauhan | last post by:
I need someone to make an example of how to create a graphics window in VB.net 2008. I understand the basics of how to draw a rectangle and lines etc. What I need is an example of how to make a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.