在Java中以特定角度绘制一条线 [英] Draw a line at a specific angle in Java

查看:155
本文介绍了在Java中以特定角度绘制一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个(x,y)对于一个行的起点始终是相同的,而(x,y)对于同一行的终点是变化的。该线也总是40px长。在程序开始时,线条以垂直方向发起(让我们称之为0度)。基于用户输入,我需要通过仅更改结束(x,y)来从其原点重新绘制特定数量的度数。

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).

SOME如果您需要更多的食物,那么:

我正在努力计算这一点并使其在Java中运行。我可以根据圆弧段的弧长来计算点数,但我不知道如何让Java做到这一点。

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.

我认为基于三角形角度它会更容易工作,因为我总是知道三角形两边的长度(一边由40px长线形成,另一边由该线的起点和JPanel的边界形成)以及这两条线形成的角度。尽管如此,我的大脑还是试图弄明白。任何帮助将不胜感激。

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.

更新:

@casablanca让我在正确的轨道上。我刷了我的触发功能,这就是我的工作方式。

@casablanca got me on the right track. I brushed up on my trig functions and here is how I made it work.

首先,我没有意识到90度是直线上升,但是一旦我做了意识到我让我的解决方案反映了这个事实。我从画框的底部中心开始画出我的画线。由于当我的用户给出的角度小于90度时,三角形的对面侧面位于屏幕的右侧,并且位于屏幕的左侧我的用户给出的角度大于90度我必须调整公式来解释这个事实,因此我有四个方法,一个用于左边的 x 坐标屏幕的一侧(当用户给定角度大于90度时),一个用于屏幕左侧的 y 坐标(当用户给定角度更大时)当用户给定角度小于90度时,对于屏幕右侧同样的事情。所有方法中的 int length 都是斜边的长度。 再次感谢您的帮助@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)画一条线重新使用。

And draw a line from (startX, startY) to (endX, endY) in whatever API you're using.

另请注意, angle 以弧度为单位。如果你有度数,你需要先转换它:

Also note that angle is in radians. If you had it in degrees, you need to convert it first:

angle = angle * Math.PI / 180;

这篇关于在Java中以特定角度绘制一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆