i need help from the c++ programmers out there...
i'm doing an assignment for my first year c++ class and it's about computer graphics
i have to write a function, that given two points (x1, y1) and (x2, y2) the function will draw a line with those endpoints.
// HINTS:
// 1) use slope fomula to calculate slope of line:
// m = rise / run = (y2 - y1) / (x2 - x1)
// Don't forget about integer vs. floating point math when dividing!!!
// 2) use the point-slope equation to compute the "between" points on the line:
// given x (x1 <= x <= x2), then y = m * (x - x1) + y1 OR
// given y (y1 <= y <= y2), then x = (y - y1) / m + x1
// 3) Either use DrawDot to draw the points on the line (for a "fat" line) OR
// draw the line directly into the buffer yourself as a series of pixels (a "thin" line)
so part one is easy;
float slope;
slope = float(y2 - y1) / float(x2 - x1);
but for part two, i'm confused as to what x and y are. also i dont get how to save the "between" points, or should i figure out the points and draw them onto the buffer all together in a loop??
any help would be much appreciated.
edit: how do you lj cut so you can change the value of "read more.."
anyways... i just wanted to thank everyone that helped, and here's the solution i FINALLY figured out...
// DEMO 4: Draw a line from (x1, y1) to (x2, y2).
// PRE: buffer[0 .. size] are defined and 0 <= x1, y1, x2, y2 < size, and colour is defined
// POST: a line from (x1, y1) to (x2, y2) has been drawn in buffer in given colour.
void DrawLine(int buffer[], int x1, int y1, int x2, int y2, int colour)
{
float y;
float slope = float(y2 - y1) / float(x2 - x1);
if (x1 - x2 == 0)
for (float y = y1; y <= y2; y++)
DrawDot(buffer, x1, y, colour);
else if (y1 - y2 == 0)
for (float x = x1; x <= x2; x++)
DrawDot(buffer, x, y1, colour);
else if (slope>0)
for (float x = x1; x <= x2; x = x + 0.1)
{
y = slope * (x - x1) + y1;
DrawDot(buffer, x, y, colour);
}
else if (slope<0)
for (float x = x2; x <= x1; x = x + 0.1)
{
y = slope * (x - x1) + y1;
DrawDot(buffer, x, y, colour);
}
}
cross-posted in computergeeks
i'm doing an assignment for my first year c++ class and it's about computer graphics
i have to write a function, that given two points (x1, y1) and (x2, y2) the function will draw a line with those endpoints.
// HINTS:
// 1) use slope fomula to calculate slope of line:
// m = rise / run = (y2 - y1) / (x2 - x1)
// Don't forget about integer vs. floating point math when dividing!!!
// 2) use the point-slope equation to compute the "between" points on the line:
// given x (x1 <= x <= x2), then y = m * (x - x1) + y1 OR
// given y (y1 <= y <= y2), then x = (y - y1) / m + x1
// 3) Either use DrawDot to draw the points on the line (for a "fat" line) OR
// draw the line directly into the buffer yourself as a series of pixels (a "thin" line)
so part one is easy;
float slope;
slope = float(y2 - y1) / float(x2 - x1);
but for part two, i'm confused as to what x and y are. also i dont get how to save the "between" points, or should i figure out the points and draw them onto the buffer all together in a loop??
any help would be much appreciated.
edit: how do you lj cut so you can change the value of "read more.."
anyways... i just wanted to thank everyone that helped, and here's the solution i FINALLY figured out...
// DEMO 4: Draw a line from (x1, y1) to (x2, y2).
// PRE: buffer[0 .. size] are defined and 0 <= x1, y1, x2, y2 < size, and colour is defined
// POST: a line from (x1, y1) to (x2, y2) has been drawn in buffer in given colour.
void DrawLine(int buffer[], int x1, int y1, int x2, int y2, int colour)
{
float y;
float slope = float(y2 - y1) / float(x2 - x1);
if (x1 - x2 == 0)
for (float y = y1; y <= y2; y++)
DrawDot(buffer, x1, y, colour);
else if (y1 - y2 == 0)
for (float x = x1; x <= x2; x++)
DrawDot(buffer, x, y1, colour);
else if (slope>0)
for (float x = x1; x <= x2; x = x + 0.1)
{
y = slope * (x - x1) + y1;
DrawDot(buffer, x, y, colour);
}
else if (slope<0)
for (float x = x2; x <= x1; x = x + 0.1)
{
y = slope * (x - x1) + y1;
DrawDot(buffer, x, y, colour);
}
}
cross-posted in computergeeks
