简体   繁体   English

如何基于两个给定点和描述圆弧段的给定高度绘制圆弧?

[英]How do I draw an arc based on two given points and a given height describing a circle segment?

I'm trying to draw an arc based on two given points and a given height describing a circle segment. 我正在尝试基于两个给定点和描述圆弧段的给定高度绘制弧。 To acomplish this I would use the following method from java.awt.Graphics. 为了实现这一点,我将使用java.awt.Graphics中的以下方法。

drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)

First I observe that the x, y, width and height values describes a rectangle containing the circle. 首先,我观察到x,y,width和height值描述了一个包含圆的矩形。

The center of the arc is the center of the rectangle whose origin is (x, y) and whose size is specified by the width and height arguments. 弧的中心是矩形的中心,其原点是(x,y),其大小由width和height参数指定。 ( http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html ) http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html

So first I start by calculating the x,y,width and height values. 首先,我首先计算x,y,width和height值。 The picture below describes how I would do this. 下图描述了我将如何做到这一点。

在此输入图像描述

The first picture shows what values I've already got. 第一张照片显示了我已经获得的价值。 The arc in the first picture is exactly what I'm trying to draw. 第一张照片中的弧线正是我想要绘制的。 In picture number two I calculate the lenght of a line between the two points. 在图片二中,我计算了两点之间一条线的长度。 My code to perform this step would look like this: 我执行此步骤的代码如下所示:

int dx = p1.x- p2.x;
int dy = p1.y - p2.y;
double len = Math.sqrt(Math.pow((double)dx, 2) + Math.pow((double)dy, 2));

As shown in picture three I can now calculate the radius of the circle by using the following function. 如图3所示,我现在可以使用以下函数计算圆的半径。

radius = h/2 + len^2 / 8h

The first problem I encounter is to calculate the CenterPoint of the circle. 我遇到的第一个问题是计算圆的中心点。 This is partly where I need help. 这部分是我需要帮助的地方。

If I was to calculate the Center Point I can then easily find the x, y, whith and height coordinates. 如果我要计算中心点,我可以很容易地找到x,y,whith和height坐标。

x = centerPoint.x - radius;
y = centerPoint.y - radius;
width = radius * 2;
height = radius * 2;

The last part is to calculate the startAngle and arcAngle based on the values we already calculated. 最后一部分是根据我们已经计算过的值计算startAngle和arcAngle。

TL;DR I need help with calculating the angles and the center point. TL; DR我需要帮助计算角度和中心点。

Thanks in advance! 提前致谢!

There's a well-known relationship (see here , for instance) between the chord half length, the height of the arc (also called the sagitta ), and the radius. 在弦长半径,弧的高度(也称为sagitta )和半径之间存在着名的关系(例如,见这里 )。 Let the chord length (the distance between p 1 and p 2 be l = 2 d , let the arc height be h , and let the radius be r . Then 设弦长(p 1和p 2之间的距离为l = 2 d ,让弧高为h ,半径为r 。然后

r = ( d 2 + h 2 ) / (2 h ) r =( d 2 + h 2 )/(2 h

The center is on the perpendicular bisector of the chord, at a distance r - h from the chord, on the opposite side from the arc. 中心位于弦的垂直平分线上,距离弦的距离为r - h ,位于弧的相对侧。 1 You can then use standard inverse trig functions to get the start and end angles for the chord. 1然后,您可以使用标准反向触发功能来获得和弦的起始和结束角度。

1 Note that it is not enough to know p 1 , p 2 , and h ; 1注意,知道p 1p 2h是不够的; you need some way of identifying which side of the chord has the center and which side has the arc. 你需要一些方法来识别和弦的哪一侧有中心,哪一侧有弧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM