问题描述
我正在尝试在矩形对象中绘制一个渐变,具有给定的角度 (Theta),其中渐变的末端接触矩形的周长.
我认为使用切线会起作用,但我无法解决这些问题.有没有我缺少的简单算法?
最终结果
所以,这将是 (angle, RectX1, RectX2, RectY1, RectY2) 的函数.我希望它以 [x1, x2, y1, y2] 的形式返回,以便渐变将绘制在正方形上.在我的问题中,如果原点为 0,则 x2 = -x1 且 y2 = -y1.但它并不总是在原点上.
让我们调用 a 和 b 矩形边,以及 (x0,y0) 矩形中心的坐标.
您需要考虑四个区域:
区域从哪里到哪里====================================================================1 -arctan(b/a) +arctan(b/a) 直角绿色三角形2 +arctan(b/a) π-arctan(b/a) 上黄色三角形3 π-arctan(b/a) π+arctan(b/a) 左绿色三角形4 π+arctan(b/a) -arctan(b/a) 下黄色三角形通过一点三角函数,我们可以获得每个区域中您想要的交点的坐标.
所以 Z0 是区域 1 和区域 3 的交点的表达式
而 Z1 是区域 2 和区域 4 交点的表达式
所需的线从 (X0,Y0) 到 Z0 或 Z1,具体取决于区域.所以记住 Tan(φ)=Sin(φ)/Cos(φ)
区域中的行开始结束======================================================================1 和 3 (X0,Y0) (X0 + a/2 , (a/2 * Tan(φ))+ Y02 和 4 (X0,Y0) (X0 + b/(2* Tan(φ)) , b/2 + Y0)请注意每个象限中 Tan(φ) 的符号,并且角度始终是从正 x 轴反时针方向测量的.
HTH!
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.
Let's call a and b your rectangle sides, and (x0,y0) the coordinates of your rectangle center.
You have four regions to consider:
Region from to Where ==================================================================== 1 -arctan(b/a) +arctan(b/a) Right green triangle 2 +arctan(b/a) π-arctan(b/a) Upper yellow triangle 3 π-arctan(b/a) π+arctan(b/a) Left green triangle 4 π+arctan(b/a) -arctan(b/a) Lower yellow triangle
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(φ)
Lines in regions Start End ====================================================================== 1 and 3 (X0,Y0) (X0 + a/2 , (a/2 * Tan(φ))+ Y0 2 and 4 (X0,Y0) (X0 + b/(2* Tan(φ)) , b/2 + Y0)
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!
这篇关于在给定角度的矩形上查找点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!