假设我的(x,y)对于一条线的起点始终是相同的,而(x,y)对于同一条线的终点却一直变化。该行也总是40px长。在程序开始时,该线以垂直方向开始(称为0度)。根据用户输入,我需要通过仅改变端点(x,y)从其原点开始重画特定数量的度数。
如果需要,可以考虑使用更多食物:
我很想计算这个并使它在Java中工作。我可以使数学基于圆弧段的弧长来计算点,但是我不知道如何使Java做到这一点。
我认为,根据三角形的角度,它会更容易工作,因为我将永远知道三角形的两个边的长度(一侧由40px长线形成,另一侧由该线的起点和边框的边界形成)。 JPanel)和那两条线形成的角度。尽管如此,我的大脑仍在试图弄清楚。任何帮助将非常感激。
更新:
@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!
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;
}
最佳答案
这是您要找的东西吗?
startX = x;
startY = y;
endX = x + 40 * Math.sin(angle);
endY = y + 40 * Math.cos(angle);
并在所使用的任何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 * Math.PI / 180;