开发者

Drawing an I-Beam based on two points

I have two Point structures and I need to draw an I-Beam based on those points, where each point represents the cross-section on either side of the I-Beam. The width of the end caps should be fixed and arbitrary.

Basically I need to draw three lines. First I'll DrawLine(Point1, Point2), then I need the math to figure out how to draw the next two开发者_开发知识库 lines on perpendicular angles so that they are centered on Point1 and Point2.

The image below shows what I need to draw based on the center line. However, this line can be at any angle. The Point1 and Point2 that connect the line can be anywhere in a 2D space.

Drawing an I-Beam based on two points


You can try playing around with LineCaps:

protected void DrawIBeam(Graphics g, Point fromPoint, Point toPoint)
{
  using (GraphicsPath hPath = new GraphicsPath())
  {
    hPath.AddLine(new Point(-5, 0), new Point(5, 0));
    CustomLineCap myCap = new CustomLineCap(null, hPath);
    myCap.SetStrokeCaps(LineCap.Round, LineCap.Round);
    using (Pen myPen = new Pen(Color.Black, 2))
    {
      myPen.CustomStartCap = myCap;
      myPen.CustomEndCap = myCap;
      g.DrawLine(myPen, fromPoint, toPoint);
    }
  }
}

and call it:

DrawIBeam(e.Graphics, new Point(10, 10), new Point(60, 60));

Drawing an I-Beam based on two points

From CustomLineCap Class


Assuming a width that's half the width of the I part of the I beam, first you find the slope of the first line you drew.

Next, you take the negative inverse of the slope, and draw a line from Point1 of length width in both directions. That's why width is half of the width you want to draw.

Finally you draw a line from Point 2 of length width in both directions.

Here's the mathematical formula for drawing a perpendicular line.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜