一个像素长线 [英] A One Pixel Long Line

查看:57
本文介绍了一个像素长线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试定义一个长度为1个单位(1个像素)的行,实际上,

只是屏幕上的一个点。但是当我使用宽笔和

适当的StartCap和EndCap(Round)绘制时,它会显示为一个点。


我能想到的一种定义1像素长行的方法,但它很丑陋,而且b $ b笨拙。

我从给定像素的X和Y坐标中减去.5首先

System.Drawing.PointF结构,并将.5添加到其他PointF结构。它好像b $ b似乎有效(见下面的代码),但感觉不是很好。


有没有标准的方法来获取一条线单位长度1?


//名称空间OnePixelLine

名称空间OnePixelLine

{

//类表格

类表格:System.Windows.Forms.Form

{

//一个不会产生输出的整数类型点

System.Drawing.Point point0 = new System.Drawing.Point(50,50);

//两个产生2像素长行的整数类型点

System.Drawing.Point point1 = new System.Drawing.Point(100,100);

System.Drawing.Point point2 = new System.Drawing.Point(101,101);


//产生1像素长行的两个浮点类型点,但它很难看

System.Drawing.PointF pointf1 = new System.Drawing.PointF(149.5 f,149.5f);

System.Drawing.PointF pointf2 = new System.Drawing.PointF(150.5f,150.5f);


// a笔

System.Drawing.Pe n pen = new System.Drawing.Pen(System.Drawing.Color.Red);


//构造函数

表格()

{


//加宽笔。删除它以检查线条的长度

pen.Width = 9;

//将StartCap和EndCap设置为Round

pen。 StartCap = System.Drawing.Drawing2D.LineCap.Roun d;

pen.EndCap = System.Drawing.Drawing2D.LineCap.Round;

//为paint做准备/>
Paint + = new System.Windows.Forms.PaintEventHandler(OnPaint);

}


// OnPaint

void OnPaint(对象a,System.Windows.Forms.PaintEventArgs b)

{


//这个什么都不输出
b.Graphics.DrawLine(pen,point0,point0);

//这会输出一条2像素长的行

b.Graphics.DrawLine(pen ,point1,point2);

//这会输出一条1像素长的行,但它很笨拙

b.Graphics.DrawLine(pen,pointf1,pointf2); < br $>
}


// Main

public static void Main()

{

System.Windows.Forms.Application.Run(new Form());

}

}

}


解决方案

" Martijn Mulder" < I @ M>在消息中写道

news:44 *********************** @ news.wanadoo.nl ...

我尝试定义一个长度为1个单位(1个像素)的线,实际上,它只是屏幕上的一个点。但是当我用宽大的笔用适当的StartCap和EndCap(Round)绘制它时,它会显示为一个点。

我可以想到一种定义1像素的方法排长队,但它很难看,而且很笨拙。
我从给定像素的X和Y坐标中减去.5的第一个
System.Drawing.PointF结构,并添加.5到其他PointF结构。
它似乎有用(见下面的代码),但感觉不好。

是否有标准方法可以获得单位长度为1的线?


你不能只画一个像素吗?

//命名空间OnePixelLine
名称空间OnePixelLine
$

//类表格
类表格:System.Windows.Forms.Form
{b / b
//一个不会产生输出的整数类型点 System.Drawing.Point point0 = new System.Drawing.Point(50,50);

//生成2像素长行的两个整数类型点
System.Drawing.Point point1 = new System.Drawing.Point(100,100);
System.Drawing.Point point2 = new System.Drawing.Point(101,101);

//生成1的两个浮点型点-pixel长行,但很难看。
System.Drawing.PointF pointf1 = new System.Drawing.PointF(149.5f,149.5f);
System.Drawing.PointF pointf2 = new System.Drawing。 PointF(150.5f,150.5f);
//一支笔
System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Red);

//构造函数
表格()
//
//加宽笔。删除它以检查线条的长度
pen.Width = 9;

//将StartCap和EndCap设置为Round
pen.StartCap = System.Drawing.Drawing2D。 LineCap.Roun d;
pen.EndCap = System.Drawing.Drawing2D.LineCap.Round;

//为油漆做准备
Paint + = new System.Windows.Forms.PaintEventHandler (OnPaint);
}

// OnPaint
void OnPaint(对象a,System.Windows.Forms.PaintEventArgs b)
{

//这个什么都不输出
b.Graphics.DrawLine(pen,point0,point0);

//这个输出一条2像素长的线
b .Graphics.DrawLine(pen,point1,point2);

//这个输出一行1像素长,但它很笨拙
b.Graphics.DrawLine(pen,pointf1,pointf2) ;
}
//主要
public static void Main()
{
System.Windows.Forms.Application.Run(new Form() );
}
}



< snip>

有没有一种标准的方法来获得单位长度为1的线?


你不能只画一个像素吗?




不,我需要它作为一条线来进行热门测试。


或者你知道一种方法来获得
1.对像素进行点击测试? (使用宽笔)

2.绘制单个像素?


" Martijn Mulder" < I @ M>在消息中写道

news:44 *********************** @ news.wanadoo.nl ...

< snip>

是否有标准的方法来获取单位长度为1的行?

难道你不能画一个像素吗?



不,我需要它作为一条线来对它进行点击测试。

或者你知道吗?的方式
1.对像素进行命中测试? (用宽笔)
2.绘制单个像素?




你是什么意思?这是框架中的功能还是你写的东西?



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());
}
}
}


解决方案

"Martijn Mulder" <i@m> wrote in message
news:44***********************@news.wanadoo.nl...

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?
Can''t you just draw a pixel?
//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());
}
}
}



<snip>

Is there a standard way to obtain a line with unit length 1?

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. to do a hit test on a pixel? (with a wide pen)
2. to draw an individual pixel?


"Martijn Mulder" <i@m> wrote in message
news:44***********************@news.wanadoo.nl...

<snip>

Is there a standard way to obtain a line with unit length 1?

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. to do a hit test on a pixel? (with a wide pen)
2. to draw an individual pixel?



What do you mean by hittest? Is this a function in the framework or
something you''ve written?



这篇关于一个像素长线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆