How to Plot a Diagonal Line in C?

In summary, the conversation discusses the problem of plotting a line between two points on an image. The code provided uses a for loop to calculate the y value at each point along the x axis, but the y2 value is causing difficulty. The two-point formula for a straight line is suggested as a solution, but the implementation is unclear. Bresenham's line algorithm is also mentioned as a potential solution.
  • #1
1alph1
7
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
  • #3
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??
 

1. What is diagonal line plotting in C?

Diagonal line plotting in C is a technique used to create a line graph with a diagonal line connecting the data points. It is commonly used in scientific and statistical data analysis to visually represent the relationship between two variables.

2. How is diagonal line plotting done in C?

To plot a diagonal line in C, you need to first define the data points with their x and y coordinates. Then, using a loop, you can plot each point on a graph by connecting them with a line using the line() function. The slope of the line can be adjusted by changing the values of the x and y coordinates.

3. What are the advantages of using diagonal line plotting in C?

Diagonal line plotting in C allows for a visual representation of the relationship between two variables, making it easier to identify any patterns or trends. It also allows for easy comparison between different data sets and can help in making predictions based on the slope of the line.

4. Are there any limitations to diagonal line plotting in C?

One limitation of diagonal line plotting in C is that it can only be used to plot data with two variables. It also assumes a linear relationship between the variables, so it may not accurately represent non-linear relationships. Additionally, it may not be suitable for large data sets as it can be time-consuming to plot each data point individually.

5. Can diagonal line plotting be used for data analysis in any field?

Yes, diagonal line plotting can be used for data analysis in various fields such as economics, social sciences, and natural sciences. It is a versatile technique that can be applied to any data set with two variables to visualize their relationship and analyze trends.

Similar threads

  • Programming and Computer Science
Replies
6
Views
874
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
5K
  • Precalculus Mathematics Homework Help
Replies
7
Views
881
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
14
Views
2K
  • Precalculus Mathematics Homework Help
Replies
17
Views
994
Replies
2
Views
299
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
126
Replies
5
Views
361
Back
Top