在给定角度查找矩形上的点
我试图在一个矩形对象中绘制一个具有给定角度(Theta)的渐变,其中渐变的末端接触矩形的周边。
我认为使用切线可以,但我无法解决问题。有没有我缺少的简单算法?
最终结果
因此,这将是 (angle, 矩形 X1, 矩形 X2, 矩形 Y1, 矩形 Y2) 的函数。我希望它以 [x1, x2, y1, y2] 的形式返回,以便渐变将绘制在正方形上。 在我的问题中,如果原点为 0,则 x2 = -x1 且 y2 = -y1。但它并不总是在原点。
I'm trying to draw a gradient in a rectangle object, with a given angle (Theta), where the ends of the gradient are touching the perimeter of the rectangle.
I thought that using tangent would work, but I'm having trouble getting the kinks out. Is there an easy algorithm that I am just missing?
End Result
So, this is going to be a function of (angle, RectX1, RectX2, RectY1, RectY2). I want it returned in the form of [x1, x2, y1, y2], so that the gradient will draw across the square.
In my problem, if the origin is 0, then x2 = -x1 and y2 = -y1. But it's not always going to be on the origin.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我们将 a 和 b 称为矩形边,将 (x0,y0) 称为矩形中心的坐标。
您需要考虑四个区域:
通过一点三角函数,我们可以获得您想要的交叉点的坐标在每个地区。
因此 Z0 是区域 1 和 3 交点的表达式
Z1 是区域 2 和 4 交点的表达式。
所需的线从 (X0,Y0) 到 Z0 或 Z1,具体取决于区域。因此,请记住 Tan(φ)=Sin(φ)/Cos(φ)
只需注意每个象限中 Tan(φ) 的符号,并且角度始终从正 x 轴逆时针方向测量。
哈!
Let's call a and b your rectangle sides, and (x0,y0) the coordinates of your rectangle center.
You have four regions to consider:
With a little of trigonometry-fu, we can get the coordinates for your desired intersection in each region.
So Z0 is the expression for the intersection point for regions 1 and 3
And Z1 is the expression for the intersection point for regions 2 and 4
The desired lines pass from (X0,Y0) to Z0 or Z1 depending the region. So remembering that Tan(φ)=Sin(φ)/Cos(φ)
Just be aware of the signs of Tan(φ) in each quadrant, and that the angle is always measured from THE POSITIVE x axis ANTICLOCKWISE.
HTH!
好吧,哇!,我终于得到了这个。
注意:我是根据贝利撒留的精彩答案得出这个结论的。如果你喜欢这个,请也喜欢他的。我所做的就是把他说的话变成代码。
这是它在 Objective-C 中的样子。它应该足够简单,可以转换为您最喜欢的语言。
此外,这是我创建的一个小测试视图,用于验证它是否有效。创建这个视图并将其放在某个地方,它将使另一个小视图在边缘快速移动。
Ok, whew!, I finally got this one.
NOTE: I based this off of belisarius's awesome answer. If you like this, please like his, too. All I did was turn what he said into code.
Here's what it looks like in Objective-C. It should be simple enough to convert to whatever your favorite language is.
In addition, here's a little test-view I created to verify that it works. Create this view and put it somewhere, it will make another little view scoot around the edge.
JavaScript 版本:
Javascript version:
根据您的图片,我假设矩形以 (0,0) 为中心,右上角为 (w,h)。然后,连接 (0,0) 到 (w,h) 的线与 X 轴形成角度 φ,其中 tan(φ) = h/w。
假设θ>0。 φ,我们正在寻找您所绘制的线与矩形顶边相交的点 (x,y)。那么 y/x = tan(θ)。我们知道 y=h,因此求解 x,我们得到 x = h/tan(θ)。
如果 θ < φ,直线与矩形右边缘相交于 (x,y)。这次,我们知道 x=w,因此 y = tan(θ)*w。
Following your picture, I'm going to assume that the rectangle is centered at (0,0), and that the upper right corner is (w,h). Then the line connecting (0,0) to (w,h) forms an angle φ with the X axis, where tan(φ) = h/w.
Assuming that θ > φ, we are looking for the point (x,y) where the line that you have drawn intersects the top edge of the rectangle. Then y/x = tan(θ). We know that y=h so, solving for x, we get x = h/tan(θ).
If θ < φ, the line intersects with the right edge of the rectangle at (x,y). This time, we know that x=w, so y = tan(θ)*w.
这个问题有一个很好的(更具编程性的 iOS / Objective-C)答案 查找 UIView 矩形上与中心点以给定角度的直线相交的 CGPoint 涉及以下步骤:
There's a good (more programmatic iOS / Objective-C) answer to this question at Find the CGPoint on a UIView rectangle intersected by a straight line at a given angle from the center point involving the following steps:
Unity C#(从 Winter 的 Java 代码转换而来)
Unity C# (Converted from Winter's Java code)
对于 Java,LibGDX。我将角度设置为双倍以提高精度。
For Java, LibGDX. I've let the angle be a double to increase precision.
虚幻引擎 4 (UE4) C++ 版本。
注意:这是基于 Olie 的代码。基于贝利撒留的答案。如果这对您有帮助,请给这些人点赞。
变化:使用UE4语法和函数,Angle被否定。
标头
代码
Unreal Engine 4 (UE4) C++ Version.
Note: This is based off of Olie's Code. Based on Belisarius's Answer. Give those guys upvotes if this helps you.
Changes: Uses UE4 syntax and functions, and Angle is negated.
Header
Code
PYTHON
PYTHON
延长矩形的四个边。
从中心发出的射线必然与两条边相交,一个交点在矩形上,一个在延长线上。而且,矩形上的点总是比延长线上的点更靠近中心。
我们的线索是光线的角度和矩形的大小。通过使用与 x 的角度和与 y 的角度,我们计算从中心到两点的距离。我们取较短的距离为R。此时,我们就有了射线的角度和长度,它们恰好形成了极坐标。将此极坐标转换为笛卡尔坐标即可得到交点的精确坐标。
您可以在 codepen 中查看 LinearGradient 示例
extending the four sides of a rectangle.
A ray emanating from the center will inevitably intersect with two sides, one intersection point on the rectangle and one on the extended line. Moreover, the point on the rectangle is always closer to the center than the one on the extended line.
Our clues are the ray's angle and the rectangle's size. By using the angle with x and the angle with y, we calculate the distances from the center to the two points. We take the shorter distance as R. At this point, we have the ray's angle and its length, which exactly form a polar coordinate. Converting this polar coordinate to a Cartesian coordinate gives us the exact coordinates of the intersection point.
You can check it in codepen for linearGradient example