我正在尝试使用给定角度(Theta)在矩形对象中绘制渐变,其中渐变的末端接触矩形的周长。
我认为使用切线会起作用,但是我很难解决问题。有一个简单的算法,我只是缺少?
最终结果
因此,这将是(angle,RectX1,RectX2,RectY1,RectY2)的函数。我希望它以[x1,x2,y1,y2]的形式返回,以便渐变将在正方形上绘制。 在我的问题中,如果原点是0,那么x2 = -x1和y2 = -y1。但它并不总是出现在原点上。
答案 0 :(得分:35)
让我们调用 a 和 b 您的矩形边,然后(x0,y0)调整矩形中心的坐标。
您需要考虑四个区域:
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
使用一点trigonometry-fu,我们可以获得每个区域中所需交点的坐标。
所以 Z0 是区域1和3的交点的表达式 并且 Z1 是区域2和4的交叉点的表达式
根据区域,所需的线从(X0,Y0)传递到Z0或Z1。所以记住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)
请注意每个象限中Tan(φ)的符号,并且始终从THE POSITIVE x轴ANTICLOCKWISE测量角度。
HTH!
答案 1 :(得分:10)
好的,哇!,我终于得到了这个。
注意:我的理由是基于belisarius的精彩答案。如果你喜欢这个,请也喜欢他。我所做的只是将他所说的内容转化为代码。
这是Objective-C中的样子。它应该很简单,可以转换为您喜欢的语言。
+ (CGPoint) edgeOfView: (UIView*) view atAngle: (float) theta
{
// Move theta to range -M_PI .. M_PI
const double twoPI = M_PI * 2.;
while (theta < -M_PI)
{
theta += twoPI;
}
while (theta > M_PI)
{
theta -= twoPI;
}
// find edge ofview
// Ref: http://stackoverflow.com/questions/4061576/finding-points-on-a-rectangle-at-a-given-angle
float aa = view.bounds.size.width; // "a" in the diagram
float bb = view.bounds.size.height; // "b"
// Find our region (diagram)
float rectAtan = atan2f(bb, aa);
float tanTheta = tan(theta);
int region;
if ((theta > -rectAtan)
&& (theta <= rectAtan) )
{
region = 1;
}
else if ((theta > rectAtan)
&& (theta <= (M_PI - rectAtan)) )
{
region = 2;
}
else if ((theta > (M_PI - rectAtan))
|| (theta <= -(M_PI - rectAtan)) )
{
region = 3;
}
else
{
region = 4;
}
CGPoint edgePoint = view.center;
float xFactor = 1;
float yFactor = 1;
switch (region)
{
case 1: yFactor = -1; break;
case 2: yFactor = -1; break;
case 3: xFactor = -1; break;
case 4: xFactor = -1; break;
}
if ((region == 1)
|| (region == 3) )
{
edgePoint.x += xFactor * (aa / 2.); // "Z0"
edgePoint.y += yFactor * (aa / 2.) * tanTheta;
}
else // region 2 or 4
{
edgePoint.x += xFactor * (bb / (2. * tanTheta)); // "Z1"
edgePoint.y += yFactor * (bb / 2.);
}
return edgePoint;
}
此外,这是我创建的一个小测试视图,用于验证它是否有效。创建此视图并将其放置在某个位置,它将在边缘处进行另一个小视图扫描。
@interface DebugEdgeView()
{
int degrees;
UIView *dotView;
NSTimer *timer;
}
@end
@implementation DebugEdgeView
- (void) dealloc
{
[timer invalidate];
}
- (id) initWithFrame: (CGRect) frame
{
self = [super initWithFrame: frame];
if (self)
{
self.backgroundColor = [[UIColor magentaColor] colorWithAlphaComponent: 0.25];
degrees = 0;
self.clipsToBounds = NO;
// create subview dot
CGRect dotRect = CGRectMake(frame.size.width / 2., frame.size.height / 2., 20, 20);
dotView = [[DotView alloc] initWithFrame: dotRect];
dotView.backgroundColor = [UIColor magentaColor];
[self addSubview: dotView];
// move it around our edges
timer = [NSTimer scheduledTimerWithTimeInterval: (5. / 360.)
target: self
selector: @selector(timerFired:)
userInfo: nil
repeats: YES];
}
return self;
}
- (void) timerFired: (NSTimer*) timer
{
float radians = ++degrees * M_PI / 180.;
if (degrees > 360)
{
degrees -= 360;
}
dispatch_async(dispatch_get_main_queue(), ^{
CGPoint edgePoint = [MFUtils edgeOfView: self atAngle: radians];
edgePoint.x += (self.bounds.size.width / 2.) - self.center.x;
edgePoint.y += (self.bounds.size.height / 2.) - self.center.y;
dotView.center = edgePoint;
});
}
@end
答案 2 :(得分:6)
Javascript版本:
function edgeOfView(rect, deg) {
var twoPI = Math.PI*2;
var theta = deg * Math.PI / 180;
while (theta < -Math.PI) {
theta += twoPI;
}
while (theta > Math.PI) {
theta -= twoPI;
}
var rectAtan = Math.atan2(rect.height, rect.width);
var tanTheta = Math.tan(theta);
var region;
if ((theta > -rectAtan) && (theta <= rectAtan)) {
region = 1;
} else if ((theta > rectAtan) && (theta <= (Math.PI - rectAtan))) {
region = 2;
} else if ((theta > (Math.PI - rectAtan)) || (theta <= -(Math.PI - rectAtan))) {
region = 3;
} else {
region = 4;
}
var edgePoint = {x: rect.width/2, y: rect.height/2};
var xFactor = 1;
var yFactor = 1;
switch (region) {
case 1: yFactor = -1; break;
case 2: yFactor = -1; break;
case 3: xFactor = -1; break;
case 4: xFactor = -1; break;
}
if ((region === 1) || (region === 3)) {
edgePoint.x += xFactor * (rect.width / 2.); // "Z0"
edgePoint.y += yFactor * (rect.width / 2.) * tanTheta;
} else {
edgePoint.x += xFactor * (rect.height / (2. * tanTheta)); // "Z1"
edgePoint.y += yFactor * (rect.height / 2.);
}
return edgePoint;
};
&#13;
答案 3 :(得分:3)
在你的照片之后,我将假设矩形以(0,0)为中心,右上角为(w,h)。然后连接(0,0)到(w,h)的线与X轴形成角度φ,其中tan(φ)= h / w。
假设θ> φ,我们正在寻找点(x,y),其中您绘制的线与矩形的上边缘相交。然后y / x = tan(θ)。我们知道y = h所以,求解x,得到x = h / tan(θ)。
如果θ< φ,该线与(x,y)处矩形的右边缘相交。这一次,我们知道x = w,所以y = tan(θ)* w。
答案 4 :(得分:1)
Find the CGPoint on a UIView rectangle intersected by a straight line at a given angle from the center point涉及以下步骤,对这个问题有一个很好的(更多程序化的iOS / Objective-C)答案:
答案 5 :(得分:0)
对于Java,是LibGDX。我将角度设为两倍以提高精度。
public static Vector2 projectToRectEdge(double angle, float width, float height, Vector2 out)
{
return projectToRectEdgeRad(Math.toRadians(angle), width, height, out);
}
public static Vector2 projectToRectEdgeRad(double angle, float width, float height, Vector2 out)
{
float theta = negMod((float)angle + MathUtils.PI, MathUtils.PI2) - MathUtils.PI;
float diag = MathUtils.atan2(height, width);
float tangent = (float)Math.tan(angle);
if (theta > -diag && theta <= diag)
{
out.x = width / 2f;
out.y = width / 2f * tangent;
}
else if(theta > diag && theta <= MathUtils.PI - diag)
{
out.x = height / 2f / tangent;
out.y = height / 2f;
}
else if(theta > MathUtils.PI - diag && theta <= MathUtils.PI + diag)
{
out.x = -width / 2f;
out.y = -width / 2f * tangent;
}
else
{
out.x = -height / 2f / tangent;
out.y = -height / 2f;
}
return out;
}
答案 6 :(得分:0)
虚幻引擎4(UE4)C ++版本。
注意:这基于Olie的Code。基于Belisarius的Answer。如果这可以帮助您,请给这些人投票。
更改:使用UE4语法和功能,并且取反了Angle。
标题
UFUNCTION(BlueprintCallable, meta = (DisplayName = "Project To Rectangle Edge (Radians)"), Category = "Math|Geometry")
static void ProjectToRectangleEdgeRadians(FVector2D Extents, float Angle, FVector2D & EdgeLocation);
代码
void UFunctionLibrary::ProjectToRectangleEdgeRadians(FVector2D Extents, float Angle, FVector2D & EdgeLocation)
{
// Move theta to range -M_PI .. M_PI. Also negate the angle to work as expected.
float theta = FMath::UnwindRadians(-Angle);
// Ref: http://stackoverflow.com/questions/4061576/finding-points-on-a-rectangle-at-a-given-angle
float a = Extents.X; // "a" in the diagram | Width
float b = Extents.Y; // "b" | Height
// Find our region (diagram)
float rectAtan = FMath::Atan2(b, a);
float tanTheta = FMath::Tan(theta);
int region;
if ((theta > -rectAtan) && (theta <= rectAtan))
{
region = 1;
}
else if ((theta > rectAtan) && (theta <= (PI - rectAtan)))
{
region = 2;
}
else if ((theta > (PI - rectAtan)) || (theta <= -(PI - rectAtan)))
{
region = 3;
}
else
{
region = 4;
}
float xFactor = 1.f;
float yFactor = 1.f;
switch (region)
{
case 1: yFactor = -1; break;
case 2: yFactor = -1; break;
case 3: xFactor = -1; break;
case 4: xFactor = -1; break;
}
EdgeLocation = FVector2D(0.f, 0.f); // This rese is nessesary, UE might re-use otherwise.
if (region == 1 || region == 3)
{
EdgeLocation.X += xFactor * (a / 2.f); // "Z0"
EdgeLocation.Y += yFactor * (a / 2.f) * tanTheta;
}
else // region 2 or 4
{
EdgeLocation.X += xFactor * (b / (2.f * tanTheta)); // "Z1"
EdgeLocation.Y += yFactor * (b / 2.f);
}
}
答案 7 :(得分:0)
蟒蛇
import math
import matplotlib.pyplot as plt
twoPI = math.pi * 2.0
PI = math.pi
def get_points(width, height, theta):
theta %= twoPI
aa = width
bb = height
rectAtan = math.atan2(bb,aa)
tanTheta = math.tan(theta)
xFactor = 1
yFactor = 1
# determine regions
if theta > twoPI-rectAtan or theta <= rectAtan:
region = 1
elif theta > rectAtan and theta <= PI-rectAtan:
region = 2
elif theta > PI - rectAtan and theta <= PI + rectAtan:
region = 3
xFactor = -1
yFactor = -1
elif theta > PI + rectAtan and theta < twoPI - rectAtan:
region = 4
xFactor = -1
yFactor = -1
else:
print(f"region assign failed : {theta}")
raise
# print(region, xFactor, yFactor)
edgePoint = [0,0]
## calculate points
if (region == 1) or (region == 3):
edgePoint[0] += xFactor * (aa / 2.)
edgePoint[1] += yFactor * (aa / 2.) * tanTheta
else:
edgePoint[0] += xFactor * (bb / (2. * tanTheta))
edgePoint[1] += yFactor * (bb / 2.)
return region, edgePoint
l_x = []
l_y = []
theta = 0
for _ in range(10000):
r, (x, y) = get_points(600,300, theta)
l_x.append(x)
l_y.append(y)
theta += (0.01 / PI)
if _ % 100 == 0:
print(r, x,y)
plt.plot(l_x, l_y)
plt.show()