1
$\begingroup$

I have usecase where I need to calculate the position of a object on a rectangle based on it's orientation as depicted below.

Where outer rectangle represents is represented with it's lower left corner located at origin and top right corner is it's width and height. Object moves around innter rectanlge with it's corresponding lower left and top right cooridnates are given by (x, y) and (x+w, y+w). I have also indicated relationship between object orientation and it's position on inner rectangle.

enter image description here One approach can be to check the orientation of the object and have following implementation

IF orientation between (45, 135):
    scaling factor = (orientation - 45) / 90
    Y  = y
    X = scaling_factor * w + x

Similary for other 3 quadrants.

Is there any better approach than this?

1 Answers 1

2

Let: $\,R =\,\text{orientation}\,\implies\, \color{red}{-180\,\le R \lt +180}\,$
If better approach means less steps, then it is possible to be done by ONE condition: $$ \boxed{ \quad \qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad \\ \begin{align} &\space\space\space \color{blue}{\text{If }\,\left(\,45\,\le\,|R|\,\le\,135\,\right)} \\[1mm] &\quad\qquad X=x+\left(\frac{|R|}{45}-1\right)\cdot\frac{w}{2} \\[1mm] &\quad\qquad Y=y-\left(\frac{|R|}{R}-1\right)\cdot\frac{w}{2} \\[2mm] &\space\space \color{blue}{\text{Else}} \\[1mm] &\quad\qquad T=|R|-90 \\[1mm] &\quad\qquad X=x+\left(\frac{|T|}{T}+1\right)\cdot\frac{w}{2} \\[1mm] &\quad\qquad Y=y+\frac{w}{2}+\frac{|R|}{R}\cdot\left(\frac{|T|}{90}-1\right)\cdot w\qquad\color{red}{\small\rightarrow\{\text{Exclud}\,R=0\}} \end{align} \\ \qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad \quad } $$ Nevertheless, if better approach means less execution-time, then the previous algorithm need a bit of utilization (more If statements -versus- less Math functions/operations).

For The Two Horizontal Cases:
$$ \begin{align} X-x &= \frac{|R|-45}{90}\times{w}=\left(\frac{|R|}{45}-1\right)\cdot\frac{w}{2} \\[2mm] Y-y &= \begin{cases} 0 &\colon [+45,\,+135]\quad\equiv R\gt0 \\ w &\colon [-45,\,-135]\quad\equiv R\lt0 \end{cases} \\[1mm] &= \frac{1-|R|/R}{2}\times{w}=-\left(\frac{|R|}{R}-1\right)\cdot\frac{w}{2} \end{align} $$ Where, we use $\small\,|R|/R\,$ to determine the sign ($\small+1$ or $\small-1$).

For The Two Vertical Cases:
The introduction of $\,\color{red}{\small T=|R|-90}\,$, is equivalent to shift both intervals to symmetric boundaries.
The new intervals are: $$ \begin{align} \left\{\,\quad[+45,\,\,\,0]\quad\text{and}\quad[-45,\,\,\,0]\quad\,\right\} &\longrightarrow\quad [-45,\,-90] \\ \left\{\,[+135,\,+180]\,\text{and}\,[-135,\,-180]\,\right\} &\longrightarrow\quad [+45,\,+90] \end{align} $$ And we have quite similar cases to the horizontals with one last trick, that is the centers of the actual intervals $\small\{0,\,-180\}$ are the boundaries of the new intervals $\small\{-90,\,90\}$ respectively!!

Pseudo-Code:

If (Abs(R) >= 45) And (135 >= Abs(R)) Then
    X = x + (Abs(R)/45 - 1) * w/2
    Y = y - (Abs(R)/R  - 1) * w/2
Else
    T = Abs(R) - 90
    X = x + (Abs(T)/T + 1) * w/2
    Y = y + w/2 + (Abs(R)/R) * (Abs(T)/90 - 1) * w


Excel-Formula:

R = Cell[A2] = { Number between -180 and +180 }
X = Cell[B2] = IF(AND(ABS(A2)>=45,135>=ABS(A2)),
                 ((ABS(A2)/45)-1)/2,((ABS(ABS(A2)-90)/(ABS(A2)-90))+1)/2)
Y = Cell[C2] = IF(AND(ABS(A2)>=45,135>=ABS(A2)),
                 (1-(ABS(A2)/A2))/2,(1/2)+(ABS(A2)/A2)*((ABS(ABS(A2)-90)/90)-1))


enter image description here

  • 0
    @Sirish: Thanks.2017-01-28