Java 用 2 个点和圆心绘制圆弧
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4196749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Draw arc with 2 points and center of the circle
提问by CarolusPl
I have two points of circle and center of this circle. I want to draw an arc between these points. Method drawArc
is to simple and doesn't fit my purpose.
Anybody help?
我有两个圆点和这个圆的中心。我想在这些点之间画一条弧线。方法drawArc
很简单,不符合我的目的。有人帮忙吗?
回答by Adamski
Graphics.drawArc
expects the following parameters:
Graphics.drawArc
期望以下参数:
- x
- y
- width
- height
- startAngle
- arcAngle
- X
- 是
- 宽度
- 高度
- 起始角
- 弧角
Given your arc start and end points it is possible to compute a bounding boxwhere the arc will be drawn. This gives you enough information to provide parameters: x, y, width and height.
鉴于您的弧起点和终点,可以计算将绘制弧的边界框。这为您提供了足够的信息来提供参数:x、y、宽度和高度。
You haven't specified the desired angle so I guess you could choose one arbitrarily.
您还没有指定所需的角度,所以我想您可以任意选择一个。
回答by botismarius
You can use Canvas.drawArc, but you must compute the arguments it needs:
您可以使用 Canvas.drawArc,但您必须计算它需要的参数:
Lets say that the center of the circle is (x0, y0) and that the arc contains your two points (x1, y1) and (x2, y2). Then the radius is: r=sqrt((x1-x0)(x1-x0) + (y1-y0)(y1-y0)). So:
假设圆的中心是 (x0, y0) 并且弧包含您的两个点 (x1, y1) 和 (x2, y2)。那么半径为:r=sqrt((x1-x0) (x1-x0) + (y1-y0)(y1-y0))。所以:
int r = (int)Math.sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));
int x = x0-r;
int y = y0-r;
int width = 2*r;
int height = 2*r;
int startAngle = (int) (180/Math.PI*atan2(y1-y0, x1-x0));
int endAngle = (int) (180/Math.PI*atan2(y2-y0, x2-x0));
canvas.drawArc(x, y, width, height, startAngle, endAngle);
Good luck!
祝你好运!