How to Plot a Diagonal Line in C?

  • Thread starter Thread starter 1alph1
  • Start date Start date
  • Tags Tags
    Line Plotting
Click For Summary

Discussion Overview

The discussion revolves around the problem of plotting a diagonal line between two points in a C program. Participants explore different methods and algorithms to achieve this, including the use of gradients and specific line-drawing algorithms.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant describes their current approach to plotting a line using a for loop and expresses uncertainty about calculating the correct y-coordinate for a diagonal line.
  • Another participant suggests using the two-point formula for a straight line, referencing an external resource for further clarification.
  • A third participant acknowledges the two-point formula but questions how to implement it in their code, specifically regarding the movement of y while keeping y1 constant.
  • Another participant proposes Bresenham's line algorithm as a potential solution for drawing the line.

Areas of Agreement / Disagreement

Participants have not reached a consensus on the best method to implement the line plotting. Multiple approaches are suggested, and there is ongoing uncertainty about the correct implementation details.

Contextual Notes

Participants express limitations in their understanding of how to apply mathematical concepts to their coding problem, particularly regarding the gradient and the implementation of line-drawing algorithms.

1alph1
Messages
5
Reaction score
0
this is quite a simple problem,what i am trying to do is plot a line between any two points on my image, i can plot horizontal lines fine.

below is the part of my code to for the line...

int x1 = 92, y1 = 35, x2 = 150, y2 = 55;

//line between two points

for (x = x1; x <= x2; x++)
{
y = y1;
y2 = y1 + ((y2-y1)/(x2-x1))*(x2-x1);
//line between two points

for (x = x1; x <= x2; x++)
{
y = y1;
y2 = y1 + ((y2-y1)/(x2-x1))*(x2-x1); ?
image[x][y][0] = 255; // red colour component
image[x][y][1] = 255; // green colour compnent
image[x][y][2] = 255; // blue colour compnent
}

}

this plots a straight line from points (x1,y1) to (x2, )
however I am stuck on the y2 value I am not sure on what code to use i know its got somthing to do with the graident?

i also know that...

about a for loop in x running between x1 and x2, and calculating a value of y at each point in x.

Something like

for (x = x1; x <= x2; x++)
{
y = y1 + gradient*(x - x1);
}

Now if the magnitude of the gradient is greater than one you'll need a loop in y calculating x at each point.

And you'll need to check x2 is greater than x1 (and swap them if it's not) or the loop won't work.

any help would be really appericated thanks so much guys!
 
Technology news on Phys.org
ok i see, yes that's a good formula but I am not sure how it would be implemented into the code? i have sex x = x1 this wouldn't work with this formula, i could pre determine the graident from the four points but I am not sure what to do to get y2 to move vertically while y1 remains constant??
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
7
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
17
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K