以给定角度在矩形上查找点 [英] Finding points on a rectangle at a given angle

查看:105
本文介绍了以给定角度在矩形上查找点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个矩形对象中绘制一个具有给定角度(Theta)的渐变,渐变的末端触及矩形的周长。





我认为使用正切会工作,但我无法解决问题。有没有简单的算法,我只是失踪?



最终结果

所以,这将是(角度,RectX1,RectX2,RectY1,RectY2)的函数。我希望它以[x1,x2,y1,y2]的形式返回,以便渐变将在广场上绘制。
在我的问题中,如果原点为0,那么x2 = -x1和y2 = -y1。但它并不总是在原点上。 b 矩形边,(x0,y0)矩形中心的坐标。



您有四个地区需要考虑:

 
Region from至Where
===== ================================================== =============
1 -arctan(b / a)+ arctan(b / a)右绿色三角形
2 + arctan(b / a)π - arctan(b / a)上面的黄色三角形
3-pictan(b / a)π+ arctan(b / a)左边的绿色三角形
4 pi + arctan(b / a) - arctan(b / a)较低的黄色三角形

使用一点三角函数fu,我们可以得到每个所需交点的坐标地区。





所以 Z0 是区域1和3的交点表达式

Z1 是区域2和4的交点

所需线根据区域从(X0,Y0)传递到Z0或Z1。所以记住Tan(&)= Sin(φ)/ Cos(φ)

 

结束
============================================= =========================
1和3(X0,Y0)(X0 + a / 2,(a / 2 * Tan (&))+ Y0
2和4(X0,Y0)(X0 + b /(2 * Tan(ph)),b / 2 + Y0)

 

只要知道每个象限中的符号(&),并且角度总是从THE POSITIVE x轴ANTICLOCKWISE测量。



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!

这篇关于以给定角度在矩形上查找点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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