J
Jay Dee Thompson
I am trying to understand the principles behind drawing to a 2D
surface.
I have created a simple Image class:
public class Image
{
internal Color[,] Pixels;
public Size size;
public int X; // references size.X
public int Y; // references size.Y
}
Then created a Graphics class:
public class Graphics
{
Image image; // the image to be manipulated.
public void DrawLine(Color colour, Point point1, Point point2)
{
// Horizontal line.
if (point1.Y == point2.Y)
{
if (point1.X > point2.X)
{
Point p = point1;
point1 = point2;
point2 = p;
}
int x = point1.X;
while (x <= point2.X)
{
this.image.pixels[x++, point1.Y] = colour;
}
}
// Vertical line.
else if (point1.X == point2.X)
{
if (point1.Y > point2.Y)
{
Point p = point1;
point1 = point2;
point2 = p;
}
int y = point1.Y;
while (y <= point2.Y)
{
this.image.pixels[point1.X, y++] = colour;
}
}
// Other
else
{
if (point1.X > point2.X || point1.Y > point2.Y)
{
Point p = point1;
point1 = point2;
point2 = p;
}
throw new Exception("currently only supports horizontal
and vertical lines");
}
}
}
What I would like to understand is how I would go about drawing a line
in any direction from point1 to point2.
Could someone explain how I would go about finishing the DrawLine
method or point me to some theory that would help me understand?
Thank you
Jay Dee
surface.
I have created a simple Image class:
public class Image
{
internal Color[,] Pixels;
public Size size;
public int X; // references size.X
public int Y; // references size.Y
}
Then created a Graphics class:
public class Graphics
{
Image image; // the image to be manipulated.
public void DrawLine(Color colour, Point point1, Point point2)
{
// Horizontal line.
if (point1.Y == point2.Y)
{
if (point1.X > point2.X)
{
Point p = point1;
point1 = point2;
point2 = p;
}
int x = point1.X;
while (x <= point2.X)
{
this.image.pixels[x++, point1.Y] = colour;
}
}
// Vertical line.
else if (point1.X == point2.X)
{
if (point1.Y > point2.Y)
{
Point p = point1;
point1 = point2;
point2 = p;
}
int y = point1.Y;
while (y <= point2.Y)
{
this.image.pixels[point1.X, y++] = colour;
}
}
// Other
else
{
if (point1.X > point2.X || point1.Y > point2.Y)
{
Point p = point1;
point1 = point2;
point2 = p;
}
throw new Exception("currently only supports horizontal
and vertical lines");
}
}
}
What I would like to understand is how I would go about drawing a line
in any direction from point1 to point2.
Could someone explain how I would go about finishing the DrawLine
method or point me to some theory that would help me understand?
Thank you
Jay Dee