我找不到在两条线之间绘制ARC的方法。我的约束是:我必须计算这个弧形笔划点。因为我正在使用InkCanvas并且我必须逐点绘制这个弧,所以我不能将任何对象放到屏幕或画布上。所以我知道我可以用PATH对象绘制任何弧并使用ArcSegment。使用这种方法是的,我可以画弧,但它不是画布上的笔画点。因此我无法删除或保存它。 无论如何,我需要逐点计算这个拱门。
我有在画布上绘制圆圈的代码,如下所示:
Stroke GetCircleStroke(int centerX, int centerY, int radiusX, int radiusY,double angletoDraw=2.0)
{
StylusPointCollection strokePoints = new StylusPointCollection();
int numTotalSteps = 180;
for (int i = 0; i <= numTotalSteps; i++)
{
double angle = angletoDraw * Math.PI * (double)i / (double)numTotalSteps;
StylusPoint sp = new StylusPoint();
//compute x and y points
sp.X = centerX + Math.Cos(angle) * radiusX;
sp.Y = centerY - Math.Sin(angle) * radiusY;
//add to the collection
strokePoints.Add(sp);
}
Stroke newStroke = new Stroke(strokePoints);
return newStroke;
}
我可以轻松地绘制圆圈,但我找不到绘制弧线的方法:(
我们知道中心点X,Y,我们知道Line1和Line2坐标。我只是不知道那是什么弧......
你能帮助我这样计算弧点吗?
答案 0 :(得分:3)
你有一些概念,如Line
/ Segment
,Point
,Circle
等。而不是弄乱一些难以理解的代码,让我们试试将问题分解为更容易消化的较小部分。
你有一个 Point 的概念,好吧,让我们实现一个:
public struct Point2D //omitted equality logic
{
public double X { get; }
public double Y { get; }
public Point2D(double x, double y)
{
X = x;
Y = y;
}
public override string ToString() => $"{X:N3}; {Y:N3}";
}
好的,我们还有细分或分隔Line
的概念:
public struct Segment2D
{
public Point2D Start { get; }
public Point2D End { get; }
public double Argument => Math.Atan2(End.Y - Start.Y , End.X - Start.X);
public Segment2D(Point2D start, Point2D end)
{
Start = start;
End = end;
}
}
最后,但并非最不重要的是,我们有 Circle 的概念:
public struct Circle2D
{
private const double FullCircleAngle = 2 * Math.PI;
public Point2D Center { get; }
public double Radius { get; }
public Circle2D(Point2D center, double radius)
{
if (radius <= 0)
throw new ArgumentOutOfRangeException(nameof(radius));
Center = center;
Radius = radius;
}
public IEnumerable<Point2D> GetPointsOfArch(int numberOfPoints, double startAngle, double endAngle)
{
double normalizedEndAngle;
if (startAngle < endAngle)
{
normalizedEndAngle = endAngle;
}
else
{
normalizedEndAngle = endAngle + FullCircleAngle;
}
var angleRange = normalizedEndAngle - startAngle;
angleRange = angleRange > FullCircleAngle ? FullCircleAngle : angleRange;
var step = angleRange / numberOfPoints;
var currentAngle = startAngle;
while (currentAngle <= normalizedEndAngle)
{
var x = Center.X + Radius * Math.Cos(currentAngle);
var y = Center.Y + Radius * Math.Sin(currentAngle);
yield return new Point2D(x, y);
currentAngle += step;
}
}
public IEnumerable<Point2D> GetPoints(int numberOfPoints)
=> GetPointsOfArch(numberOfPoints, 0, FullCircleAngle);
}
研究GetPointsOfArch
的实施,不应该太难理解。
现在,要解决您的问题,您可以这样做:
var myCircle = new Circle2D(new Point2D(centerX, centerY), radius);
var line1 = ....
var line2 = ....
var archPoints = myCircle.GetPointsOfArch(number, line2.Argument, line1.Argument);
阅读,遵循和理解不是那么容易吗?