Re: A One Pixel Long Line



Can't you just draw a pixel?

No, I need it as a line to do hit testing on it.
Or do you know a way to
1. do a hit test on a pixel?
2. draw an individual pixel?

The individual pixel I want to 'hit' is not part of a line, as I initially
thought, but the sole inhabitant of a System.Drawing.Rectangle object, as in
the code below. See if you are man (or woman) enough to hit the pixel with
the mouse pointer :-)



//namespace HitPixel
namespace HitPixel
{


//class Form
class Form:System.Windows.Forms.Form
{


//the pixel that we want to hit, the 'hitpixel'
System.Drawing.Point point=new System.Drawing.Point(150,150);


//line 1, arrowshaped, pointing towards 'hitpixel'
System.Drawing.Point point1=new System.Drawing.Point(150,50);
System.Drawing.Point point2=new System.Drawing.Point(150,140);


//line 2, arrowshaped, pointing towards 'hitpixel'
System.Drawing.Point point3=new System.Drawing.Point(150,250);
System.Drawing.Point point4=new System.Drawing.Point(150,160);


//line 3, arrowshaped, pointing towards 'hitpixel'
System.Drawing.Point point5=new System.Drawing.Point(50,150);
System.Drawing.Point point6=new System.Drawing.Point(140,150);


//line 4, arrowshaped, pointing towards 'hitpixel'
System.Drawing.Point point7=new System.Drawing.Point(250,150);
System.Drawing.Point point8=new System.Drawing.Point(160,150);


//the graphicspath. Holds nothing more than the 'hitpixel'
System.Drawing.Drawing2D.GraphicsPath graphicspath=new
System.Drawing.Drawing2D.GraphicsPath();


//a Black pen
System.Drawing.Pen blackpen=new
System.Drawing.Pen(System.Drawing.Color.Black,20);


//a Red pen
System.Drawing.Pen redpen=new
System.Drawing.Pen(System.Drawing.Color.Red,20);


//a pen
System.Drawing.Pen pen;



//constructor
Form()
{

//set style for double buffered graphics
SetStyle
(
System.Windows.Forms.ControlStyles.AllPaintingInWmPaint|
System.Windows.Forms.ControlStyles.DoubleBuffer|
System.Windows.Forms.ControlStyles.ResizeRedraw|
System.Windows.Forms.ControlStyles.UserPaint,
true
);


//set EndCaps of both pens to ArrowAnchor
blackpen.EndCap=System.Drawing.Drawing2D.LineCap.ArrowAnchor;
redpen.EndCap=System.Drawing.Drawing2D.LineCap.ArrowAnchor;


//set pen
pen=blackpen;


//add Rectangle to graphicspath. Rectangle is 1x1 pixel
graphicspath.AddRectangle
(
new System.Drawing.Rectangle
(
point.X,
point.Y,
1,
1
)
);


//prepare for mouse-input
MouseMove+=new System.Windows.Forms.MouseEventHandler(OnMouseMove);


//prepare for paint
Paint+=new System.Windows.Forms.PaintEventHandler(OnPaint);
}



//OnMouseMove
void OnMouseMove(object a,System.Windows.Forms.MouseEventArgs b)
{

//use Black pen
pen=blackpen;

if(graphicspath.IsOutlineVisible(b.X,b.Y,System.Drawing.Pens.Black))
{

//if pixel is hit, use Red pen
pen=redpen;
}

//force redraw
Invalidate();
}



//OnPaint
void OnPaint(object a,System.Windows.Forms.PaintEventArgs b)
{
b.Graphics.DrawLine(pen,point1,point2);
b.Graphics.DrawLine(pen,point3,point4);
b.Graphics.DrawLine(pen,point5,point6);
b.Graphics.DrawLine(pen,point7,point8);
b.Graphics.FillPath(System.Drawing.Brushes.Black,graphicspath);
}



//Main
public static void Main()
{
System.Windows.Forms.Application.Run(new Form());
}
}
}




.



Relevant Pages

  • Re: difference between drawrectangle and fillrectangle
    ... anti-aliasing. ... is at pixel 10.5, and the center of coordinate 12 is at pixel 12.5, the pen ... If you draw with SmoothingMode set to AntiAlias, you will see the fill now ... pens always draw a figure that is increased in width and height ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Pen Width less than one pixel.
    ... Thanks James. ... I have been playing around with lines and pens a bit and wanted a ... default the pen width to 1 pixel as a minimum. ... line using AntiAlias and HighQuality Smoothing modes. ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: contour shading
    ... Determine the level at the current pixel, ... Locate two distance measures to the level below, ... to use a 2d pixel traversal algorithm which tested a "ray". ... If the ray hit a pixel with the desired level, ...
    (comp.graphics.algorithms)
  • Re: Clear Pixel Tracking
    ... Tracking hits to 1x1 pixel is not really that reliable at all. ... would strip your 1x1 pixel images happily off and you would not see any hit ...
    (alt.html)
  • Re: How to make scale transform not affect line thickness
    ... we use 2 classifications of line weight. ... Line Weight = Width of line in pixels at all times, regardless of zoom. ... create pens with a width of -1. ... > provide a 1 pixel thick line regardless of the page scale settings. ...
    (microsoft.public.dotnet.framework.drawing)

Quantcast