Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>EDIT: the correct solution for rectangles. It even does not crash if width or height are zeros! Language: C++.</p> <p>tan(89.99999) thanks to James Fassett for testing my code.</p> <pre><code>#include &lt;cstdio&gt; #include &lt;math.h&gt; // declare nonstandard signum function double sign(double x); //define pi because I forgot where it's declared double const pi = 3.14159; //declare non-standard contangent function double cot(double x); int main() { for (double angle = 0.0 ; angle&lt;2*pi; angle += 0.1){ //angle should be within [0, 2*pi) range //x and y point to the _middle_ of the rectangle double x = 0; double y = 0 ; double width = 1, height = 4; double base_angle = atan(height/width); // the angle between rectangle diagonal and Ox axis double px,py; // Which side we're on? bool left = (fabs(angle - pi) &lt; base_angle); bool right = (angle&gt; 2*pi-base_angle || angle &lt; base_angle); bool top = (fabs(angle - pi/2) &lt;= fabs(pi/2 - base_angle)); bool bottom = (fabs(angle - 3*pi/2) &lt;= fabs(pi/2 - base_angle)); // The helper values used to adjust sides int lr = (left?-1:0) + (right?1:0); int tb = (bottom?-1:0) + (top?1:0); if (lr) { // we're on vertical edge of rectangle px = x+width/2*lr; py = y+width/2*tan(angle)*lr; } else { // we're on the horizontal edge or in the corner px = x+height/2*cot(angle)*tb; py = y+height/2*tb; } printf(" a = %d deg: x = %lf; y = %lf\n",(int)(angle/pi*180),px,py); } return 0; } // define nonstandard signum function double sign(double x) { if (x&lt;0) return -1; if (x&gt;0) return 1; return 0; } //define non-standard contangent function double cot(double x) { return tan(pi/2 - x); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload