I try to define a line with the length of 1 unit (1 pixel) that is, in fact,
just a point on the screen. But when I draw it with a wide pen with the
appropriate StartCap and EndCap (Round), it will appear as a dot.
I can think of a way to define a 1-pixel long line but it is ugly and
clumsy.
I subtract .5 from the given pixel's X and Y coordinate for the first
System.Drawing.PointF structure, and add .5 to the other PointF stucture. It
seems to work (see the code below), but it doesn't feel good.
Is there a standard way to obtain a line with unit length 1?
//namespace OnePixelLine
namespace OnePixelLine
{
//class Form
class Form:System.Windows.Forms.Form
{
//one integer type point that won't produce output
System.Drawing.Point point0=new System.Drawing.Point(50,50);
//two integer type points that produce 2-pixels long line
System.Drawing.Point point1=new System.Drawing.Point(100,100);
System.Drawing.Point point2=new System.Drawing.Point(101,101);
//two float type points that produce 1-pixel long line, but it is ugly
System.Drawing.PointF pointf1=new System.Drawing.PointF(149.5f,149.5f);
System.Drawing.PointF pointf2=new System.Drawing.PointF(150.5f,150.5f);
//a pen
System.Drawing.Pen pen=new System.Drawing.Pen(System.Drawing.Color.Red);
//constructor
Form()
{
//widen pen. Remove this to check the length of the lines
pen.Width=9;
//set StartCap and EndCap both to Round
pen.StartCap=System.Drawing.Drawing2D.LineCap.Roun d;
pen.EndCap=System.Drawing.Drawing2D.LineCap.Round;
//prepare for paint
Paint+=new System.Windows.Forms.PaintEventHandler(OnPaint);
}
//OnPaint
void OnPaint(object a,System.Windows.Forms.PaintEventArgs b)
{
//this outputs nothing
b.Graphics.DrawLine(pen,point0,point0);
//this outputs a line 2 pixels long
b.Graphics.DrawLine(pen,point1,point2);
//this outputs a line 1 pixel long, but it is clumsy
b.Graphics.DrawLine(pen,pointf1,pointf2);
}
//Main
public static void Main()
{
System.Windows.Forms.Application.Run(new Form());
}
}
}