424,949 Members | 946 Online Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

graphics in c

 P: n/a 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 #include 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 Replies

 P: n/a On 2007-08-12, Raj

 P: n/a On Sun, 12 Aug 2007 10:32:41 -0700, Raj wrote: On Aug 12, 9:46 pm, Ben Bacarisse Raj

 P: n/a 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 #include 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=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=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

 P: n/a On Aug 13, 4:13 pm, Raj

 P: n/a On Aug 13, 7:24 pm, "Very.Little.Gravitas.Indeed"

 P: n/a On Aug 13, 4:32 pm, Raj 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

 P: n/a Raj wrote: On Aug 13, 7:24 pm, "Very.Little.Gravitas.Indeed" On Aug 13, 4:13 pm, Raj

 P: n/a On Aug 12, 4:53 am, Raj #include 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: n/a P.S. Posts about how to do graphics (in any language) are better targeted to news:comp.graphics.algorithms Aug 13 '07 #10

 P: n/a 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=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

 P: n/a 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 #include 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. -- Posted via a free Usenet account from http://www.teranews.com Aug 14 '07 #12 