li> 根据需要扩大间距以适合线长 expanding the line width by half a pen width on both sides so the outer edges meetexpanding the gaps as needed to fit in the line length这里是线条绘制功能;它需要一个 Graphics 对象,一个 Pen 对象,两个末端的 Points 和 byte 来告诉我们该行是应该独立运行还是将连接连接到其他行,如本例所示。 Here is the line drawing function; it takes the Graphics object, a Pen, the two end Points and a byte that tells us if the line is meant to stand alone or will connect to other lines, as in our example cases. 要建立良好的连接,使其与半透明画笔一起使用,我们需要能够跳过在开头或结尾或什至两者都加一个点a,例如当我们想在下面的测试中插入一条对角线时。To make a good connection, that will work well with semi-tranparent brushes we need the ability to skip a dot a at the beginning or end or even both, e.g. when we want to insert a diaogonal line as in my test below.跳过值是 0 以跳过任何一条, 1或2 跳过第一个或最后一个点,而 3 跳过两个点。当然,您可能想使用枚举。The skip values are 0 to skip none, 1 or 2 to skip the 1st or last dot and 3 to skip both. Of course you may want to use an enumeration instead.void DrawDottedLine(Graphics g, Pen pen_, Point pa_, Point pb, byte skipDots){ float pw = pen_.Width; float pw2 = pen_.Width / 2f; Pen pen = (Pen)pen_.Clone(); // find out directions: int sigX = Math.Sign(pb_.X - pa_.X); int sigY = Math.Sign(pb_.Y - pa_.Y); // move the end points out a bit: PointF pa = new PointF(pa_.X - pw2 * sigX, pa_.Y - pw2 * sigY); PointF pb = new PointF(pb_.X + pw2 * sigX, pb_.Y + pw2 * sigY); // find line new length: float lw = (float)(Math.Abs(pb.X - pa.X)); float lh = (float)(Math.Abs(pb.Y - pa.Y)); float ll = (float)(Math.Sqrt(lw * lw + lh * lh)); // dot length: float dl = ll / pw; // dot+gap count: round to nearest odd int: int dc = (int)(2 * Math.Round((dl + 1) / 2) - 1); // gap count: int gc = dc / 2 ; // dot count: dc = gc + 1; // gap scaling float gs = (ll - dc * pw) / (pw * gc); // our custom dashpattern pen.DashPattern = new float[] { 1, gs }; // maybe skip 1st and/or last dots: if (skipDots % 2 == 1) pa = new PointF(pa_.X + pw * sigX, pa_.Y + pw * sigY); if (skipDots > 1) pb = new PointF(pb_.X - pw * sigX, pb_.Y - pw * sigY); // finally we can draw the line: g.DrawLine(pen, pa, pb); // dispose of pen clone pen.Dispose();}经过一些明显的准备后,我将分数略微移开,然后计算出数字点和垂直线或水平线的间隙。然后我计算出修改后的间隙比例。After some obvious preparations I move the points out a bit and then calculate the number of dots and of gaps for vertical or horizontal lines. Then I calculate the modified the gap scale.这里是放大4倍的结果,它绘制了4条线以形成一个矩形,矩形的笔宽从 1/3-10/3 :Here is the result, scaled up 4x, of drawing four lines to form a rectangle with varying pen widths going from 1/3 - 10/3: 这是我使用的测试床;请注意使用半透明的黑色来说明如何正确绘制角,即不重叠:This is the testbed I used; note the use of semi-transparent black to illustrate how the corners are drawn correctly, i.e. non-overlapping:Pen Dot = new Pen(Color.Black, 1f);Point pa = new Point(10, 50);Point pb = new Point(70, 50);Point pc = new Point(70, 100);Point pd = new Point(10, 100);for (int i = 1; i < 10; i++ ){ Dot = new Pen(Color.FromArgb(128, Color.Black), i / 3f){ DashStyle = DashStyle.Dot }; g.TranslateTransform(10, 10); DrawDottedLine(g, Dot, pa, pb, 2); DrawDottedLine(g, Dot, pb, pc, 2); DrawDottedLine(g, Dot, pc, pd, 2); DrawDottedLine(g, Dot, pd, pa, 2); DrawDottedLine(g, Dot, pd, pb, 3);}我真的希望有人可以通过使用 DrawLines ,但这没用,弄清楚这个解决方案后,我并不感到惊讶。I really wish one could avoid the connection problems by simply using DrawLines, but this didn't work and after figuring out this solution I'm not really amazed it didn't.. 这篇关于两端画线偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!