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! 11 2273
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
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
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...
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 :)
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...
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.
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
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;
}
}
}
P.S.
Posts about how to do graphics (in any language) are better targeted
to news:comp.graphics.algorithms
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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()...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |