在Java中以特定角度画一条线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3536428/
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 a line at a specific angle in Java
提问by ubiquibacon
Let's say I have an (x,y) that is always the same for the start point of a line and an (x,y) that changes for the end point of that same line. The line is also always 40px long. At the start of the program the line originates in a vertical orientation (lets call it 0 degrees). Based on a user input I need the line to be redrawn a specific number of degrees from its origin by changing only the end (x,y).
假设我有一个 (x,y) 对于一条线的起点总是相同的,而一个 (x,y) 对于同一条线的终点而改变。这条线也总是 40px 长。在程序开始时,这条线起源于垂直方向(我们称之为 0 度)。根据用户输入,我需要通过仅更改末端 (x,y) 来从原点重新绘制特定度数的线。
SOME MORE FOOD FOR THOUGHT IF YOU NEED IT:
如果您需要,请考虑更多食物:
I'm in a rut trying to calculate this and make it work in Java. I can make the math work to calculate the point based on the arc length of a circle segment, but I don't know how to make Java do it.
我正在努力计算这个并使其在Java中工作。我可以让数学工作来根据圆弧段的弧长计算点,但我不知道如何让 Java 做到这一点。
I think it would work easier based off a triangle angles since I will always know the length of two sides of a triangle (one side formed by the 40px long line and the other side formed by the start point of that line and the border of the JPanel) and the angle those two lines form. Still, my brain is mush from trying to figure it out. Any help would be much appreciated.
我认为基于三角形角度它会更容易工作,因为我将始终知道三角形的两条边的长度(一条边由 40px 长线形成,另一边由该线的起点和边界形成JPanel) 和这两条线形成的角度。尽管如此,我的大脑还是无法弄清楚。任何帮助将非常感激。
UPDATE:
更新:
@casablanca got me on the right track. I brushed up on my trig functions and here is how I made it work.
@casablanca 让我走上正轨。我复习了我的触发函数,这是我如何使它工作的。
First off, I didn't realize that 90 degrees was straight up, but once I did realize that I made my solution reflect that fact. I was drawing my line starting at the bottom center of the frame going out. Since the opposite
side of the triangle is on the right side of the screen when the angle given by my user is less than 90 degrees and is on the left side of the screen when the angle given by my user is greater than 90 degrees I had to adjust the formula to account for that fact, thus I have four methods, one for the x
coordinate on the left side of the screen (when the user given angle is greater than 90 degrees), one for the y
coordinate on the left side of the screen (when the user given angle is greater than 90 degrees) and the same thing for the right side of the screen when the user given angle is less than 90 degrees. The int length
in all methods is the length of the hypotenuse. Thanks again for your help @casablanca!
首先,我没有意识到 90 度是直的,但是一旦我意识到我的解决方案反映了这个事实。我从框架的底部中心开始画我的线。因为opposite
当我的用户给出的角度小于 90 度时三角形的边在屏幕的右侧,当我的用户给出的角度大于 90 度时它在屏幕的左侧我不得不调整公式以说明这一事实,因此我有四种方法,一种用于x
屏幕左侧的坐标(当用户给定的角度大于 90 度时),一种用于y
屏幕左侧的坐标(当用户给定的角度大于 90 度时),当用户给定的角度小于 90 度时,屏幕右侧的坐标也是如此。在int length
所有方法是斜边的长度。 再次感谢您的帮助@casablanca!
public double leftSideX(double angle, int length){
double x = frameWidth/2 - (length * Math.cos(Math.toRadians(90-(Math.toDegrees(angle)-90))));
return x;
}
public double leftSideY(double angle, int length){
double y = frameHeight - (length * Math.sin(Math.toRadians(90-(Math.toDegrees(angle)-90))));
return y;
}
public double rightSideX(double angle, int length){
double x = frameWidth/2 + (length * Math.cos(angle));
return x;
}
public double rightSideY(double angle, int length){
double y = frameHeight - (length * Math.sin(angle));
return y;
}
采纳答案by casablanca
Is this what you're looking for?
这是你要找的吗?
startX = x;
startY = y;
endX = x + 40 * Math.sin(angle);
endY = y + 40 * Math.cos(angle);
And draw a line from (startX, startY) to (endX, endY) in whatever API you're using.
并在您使用的任何 API 中从 (startX, startY) 到 (endX, endY) 画一条线。
Also note that angle
is in radians. If you had it in degrees, you need to convert it first:
另请注意,angle
以弧度为单位。如果你有度数,你需要先转换它:
angle = angle * Math.PI / 180;