answersLogoWhite

0


Best Answer

// macros for simplicity

#define MAX(x,y) (x>y?x:y)

#define MIN(x,y) (x<y?x:y)

/*

** drawLine

**

** Draw a line from vertex (x0,y0) to vertex (x1,y1) using

** the midpoint line algorithm, implemented using OpenGL.

**

*/

void drawLine( GLint x0, GLint y0, GLint x1, GLint y1 ) {GLint dE, dNE, x, y, d, dx, dy;

// check if we need to switch the points

if( x0 > x1 ) {

x0 = x0 + x1;

x1 = x0 - x1;

x0 = x0 - x1;

y0 = y0 + y1;

y1 = y0 - y1;

y0 = y0 - y1;}

// calculate deltas

dy = y1 - y0; dx = x1 - x0;

// special cases

if( dx -1 - diag down-right

glBegin(GL_POINTS);

for( x = x0, y = y0; x <= x1; x++, y-- ) {

glVertex2i(x,y);}

glEnd();}else { // general cases

// midpoint algorithm

if( abs(dy) < dx ) { // small slope

dE = 2 * abs(dy);

dNE = 2 * (abs(dy) - dx);

d = dE - dx;

glBegin(GL_POINTS);

for( x = x0, y = y0; x <= x1; x++ ) {

glVertex2i(x,y);

if( d <= 0 ) {

d+= dE;}else {

y += (dy>0?1:-1);

d += dNE;}}// for x = x0 to x1

glEnd();}else { // large slope

dE = 2 * dx;

dNE = 2 * (dx - abs(dy));

d = dE - abs(dy);

glBegin(GL_POINTS);

for( x = x0, y = y0; (y0 < y1 && y <= y1)

(y0 > y1 && y >= y1); y+=(y0 < y1?1:-1) ) {

glVertex2i(x,y);

if( d <= 0) { x ++;

d+= dE; }else { d += dNE; }}// for y = y0 to y1

glEnd();}}

}// drawLine()

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

The best known method for drawing a line is with Bresenham's line algorithm. An example of it in C is as follows, with the helper functions "sgn", which returns the sign of an integer, and "putpixel", which draws a pixel at the specified location:

void line(int x1, int y1, int x2, int y2){

int n, deltax, deltay, sgndeltax, sgndeltay, deltaxabs, deltayabs, x, y, drawx, drawy;

deltax = x2 - x1;

deltay = y2 - y1;

deltaxabs = abs(deltax);

deltayabs = abs(deltay);

sgndeltax = sgn(deltax);

sgndeltay = sgn(deltay);

x = deltayabs >> 1;

y = deltaxabs >> 1;

drawx = x1;

drawy = y1;

putpixel(drawx, drawy);

if(deltaxabs >= deltayabs){

for(n = 0; n < deltaxabs; n++){

y += deltayabs;

if(y >= deltaxabs){

y -= deltaxabs;

drawy += sgndeltay;

}

drawx += sgndeltax;

putpixel(drawx, drawy);

}

}else{

for(n = 0; n < deltayabs; n++){

x += deltaxabs;

if(x >= deltayabs){

x -= deltayabs;

drawx += sgndeltax;

}

drawy += sgndeltay;

putpixel(drawx, drawy);

}

}

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Code in c to draw line?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

C program to Draw a line chart?

draw a line chart.


How do you write 5.267?

first you draw a horizontal line, the on the left side of the line draw a line going down, then at the end of the line draw a curve which looks like a inverted c. Then you have 5 then put a dot on the bottom side of the 5 then draw a curve which looks like and inverted c, then on the bottom draw a line which goes horizontally, now you have 5.2 then you make a curve which looks like a sphere but the end of the line touches the middle of the line, forming a loop. Now you have 5.26 then draw a line horizontally, then at the right side draw a line which goes diagonally. Now you have wrote 5.267


How do you draw line AB and Line AC in Geometry?

You draw an angle and its vertex should be A on its left B and on its right C. ( An Acute)Confirmed


Which way is a diagnol line go?

diagonal is like this a. b. c. d. b. &amp; c. if i draw a line to them that will be a diagonal line same thing with a. &amp; d.


How do you create a parallelogram?

Draw a straight horizontal line, name A and B from A take a vertical line slight bend and name it C. From B draw a line similar and same as AC and name it D. Now join C and D. Ur parallelogram is ready.


What is wrong with this C code?

There is an error in line 42.


How do you draw a line segment and name it CD?

Draw any finite line. Write C near one end; write D near the other end. You now have a line segment and have named it CD.


Where is Middle C on treble clef staff?

You need to draw a small supplementary line below the staff. Then the middle C will be right on top of it.


How would you draw a perpendicular line?

Using a compass and ruler: # At the point you want the perpendicular line to cross, place a dot. Label it A. # Draw a circle centred at A with a radius of about an inch (two-three centimetres). Label the points it intersects the original line as B and C. # Construct two circles centred at B and C, large enough that they almost reach the other point and have the same size. Label the points they intersect at as D and E. # Draw a straight line between E and D. This line is the perpendicular line. This method can also be used to draw a perpendicular bisector, if the ends of the line you wish to bisect are labelled as points B and C.


How do you divide a triangle into 2 triangles and 2 trapezoids?

Let's assume the triangle has points A, B, and C. Method 1 (3 lines) Draw two lines across the triangle parallel to line segment AB. Now you have two trapezoids and one triangle. Draw another line from C to the any point on the closest of the two lines you just drew, splitting the triangle into two more triangles. Method 2 (2 lines) Draw one line across the triangle parallel to line segment AB. Now you have one trapezoid and one triangle. Draw a second line that passes through C and is perpendicular to AB, splitting the trapezoid into two trapezoids and the triangle into 2 triangles. Method 3 (3 lines) Draw one line from point C to any point on line segment AB. Then draw a line parallel to AC and one parallel to BC, but don't let them cross the line you just drew.


How do you draw a finite line?

draw a line that ends.


How do you construct angle120 degrees with using a compass?

First draw a horizontal straight line a b Put compass point on a (open compass approximately half the length of line a b - note: ensure the compass is not altered throughout the following). Draw an arc that cuts line a b at c. Put compass point on c and draw an arc from a to cut the first arc at d. Draw a straight line through a d to form a 60 degree angle. For a 120 degree angle, put compass point on dand draw an arc from a to cut the first arc at e. Draw a straight line from a through e to give a second angle of 60 degrees: 60 + 60 = 120 degrees.