2012-04-30 29 views
1

比方說,我有2個點C#畫線,以圓緣

Point p1 = new Pen(100, 100); 
Point p2 = new Pen(200, 150); 

而且我畫橢圓與給定的半徑點,該點是橢圓形的中心。

int radius = 5; 
RectangleF rectangle = new RectangleF(); 
rectangle.Width = radius * 2; 
rectangle.Height = radius * 2; 
rectangle.X = Convert.ToSingle(p1.X - radius); 
rectangle.Y = Convert.ToSingle(p1.Y - radius); 
g.FillEllipse(brush, rectangle); 
rectangle.X = Convert.ToSingle(p2.X - radius); 
rectangle.Y = Convert.ToSingle(p2.Y - radius); 
g.FillEllipse(brush, rectangle); 

g.DrawLine(pen, p1, p2); 

如果我在這些點之間畫線,我會得到從一箇中心到另一箇中心的線。 目前我可以忍受這一點,但我想說,那條線從Ellipse的邊緣開始,所以它不會穿過它。我怎麼能做到這一點?

回答

2

找到答案:

public static PointF getPointOnCircle(PointF p1, PointF p2, Int32 radius) 
    { 
     PointF Pointref = PointF.Subtract(p2, new SizeF(p1)); 
     double degrees = Math.Atan2(Pointref.Y, Pointref.X); 
     double cosx1 = Math.Cos(degrees); 
     double siny1 = Math.Sin(degrees); 

     double cosx2 = Math.Cos(degrees + Math.PI); 
     double siny2 = Math.Sin(degrees + Math.PI); 

     return new PointF((int)(cosx1 * (float)(radius) + (float)p1.X), (int)(siny1 * (float)(radius) + (float)p1.Y)); 
    } 
1

你有2種選擇,

1)首先劃清界線,並簡單地用FillEllipse

2覆蓋它)移位行的開始和結束位置。

移動你需要的直線位置:
a)計算中心之間的夾角。
- 這是THETA = TAN-1(Y2-Y1/X2-X1)
如果使用實際橢圓而非圓形:
b)計算橢圓該角的半徑。
這是r(θ)=(x * y)/ Sqrt(x * Cos(θ)^ 2 + y * sin(θ)^ 2)
c)計算線的偏移量。
- 這是X = RCOS(西塔)和y = RSIN(西塔)

+0

我會嘗試第二個選項,因爲使用AdjustableArrowCaps IM。 – Wish