By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 946 Online
Bytes IT Community
+ Ask a Question
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
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
Share this Question
Share on Google+
11 Replies


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

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

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

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

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

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

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

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

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 <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 discussion thread is closed

Replies have been disabled for this discussion.