# I read the first mail and find an algorithm for the special case of
"moving point particles in  a 2D pentagon boundary". The answer is
given as using pyode script but I am excited about my solution and want
to share it with you :-)
<br># The easy part of using a rectangular boundary (of which sides
are parallel to the axes) is that, one should only check if x position
of a particle is greater than x_max (coordinate of the right side of
the rectangle), if x&lt;x_min, y&lt;y_min and y&gt;y_max. And if it
exceeds the boundary in the next step of the simulation according to
ODE (or any kind of calculation) of the simulation, then reverse the
velocity in that direction (and mirror the coordinate using the
boundary line as the axis of symmetry).
<br># So, We can try to use this simple idea in pentagon case.<br># We can divide the pentagon in five triangles: Put a point in the center and draw lines to vertexes.<br><a href="http://www.math.union.edu/%7Edpvc/courses/1999-00/MTH012-02-WI00/notes/DividedPentagon.jpg" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">

http://www.math.union.edu/~dpvc/courses/1999-00/MTH012-02-WI00/notes/DividedPentagon.jpg</a><br># Call the bottom triangle 0th triangle, turn in counter clockwise direction and call other triangles as 1st, 2nd...<br># The algorithm is as following:
<br># First determine in which triangle the particle is. One can get
this using an atan2(x,y) like function. atan2 returns the angle between
the position of the particle and the x-axis. We can beforehand
calculate the angles between the sides of triangles and the x-axis. And
using this information we can find in which triangle the particle is.
(or maybe y/x is just enough for determination)
<br># If it is in the zeroth triangle, all we have to do is to look
for the y position. If it is less than minus of the height of the
bottom triangle then it exceeds the boundary. Apply the reflection like
in a rectangular boundary.
<br># One can not do this in other triangles (1st, 2nd...). Because
their boundaries are not parallel to axes but they have slopes. So, if
we rotate the coordinate system, until the bottom side of the triangle
becomes parallel to y-axis, e.g.
rotate 72 degrees clock wise for 1st triangle, 2*72 degrees for the
2nd..., (360/5=72) we can apply this reflection law. (this is the trick)<br># After applying the reflection one must rotate the coordinate system counter clock wise.
<br># Here is the formula of rotating a coordinate system (taken from
Arfken, Mathematical Methods for Physicists). (x,y) is the coordinate
in the old system and (x&#39;, y&#39;) is the coordinate in the new (rotated)
system, a is the angle of rotation.
<br><br>x&#39; = x*cos(a) + y*sin(a)<br>y&#39; = -x*sin(a) + y*cos(a)<br><br>#
But one don&#39;t have to make trigonometric calculations for every
rotation, because a is constant. It is 72 or integer multiples of 72
and one can calculate sines and cosines beforehand e.g. sin(72) =
0.9511, cos(72)=0.3090, for CCW rotations, and sin(-72)=-0.9511,
cos(-72)=0.3090 for CW rotations.<br># Here is the pseudo code:<br><br>take_the_simulation_one_step_further()<br><div id="mb_10">for_each_particle:<br>n = the_number_of_triangle_which_includes_the_particle(x,y)
<br>(x&#39;,y&#39;,vx&#39;,vy&#39;)=rotate_the_velocity_and_coordinate_of_the_particle(-n*72,x,y,vx,vy)<br>(x&#39;,ynew&#39;,vx&#39;,vynew&#39;)=reflect_the_particle_(if_necessary)_as_if_it_reflects_from_the_bottom_of_a_rectangle(x&#39;,y&#39;,vx&#39;,vy&#39;)
<br>(x,y,vx,vy)=rotate_the_velocity_and_coordinate_of_the_particle(n*72,x&#39;,ynew&#39;,vx&#39;,vynew&#39;)<br><br># I didn&#39;t try this of course, but seems plausible. Thanks for reading :-)<br><span class="sg">-uğur-
</span></div><br><br><div><span class="gmail_quote">On 10/13/07, <b class="gmail_sendername">marius schebella</b> &lt;<a href="mailto:marius.schebella@gmail.com">marius.schebella@gmail.com</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
hi,<br>I need to bounce objects against a border that is not running parallel<br>to x or y, but in a gradient angle. actually I need to bounce the<br>objects within a pentagon. iSeg2D allows object to cross the border when
<br>they bounce, there is no x/y-max for gradients.<br>I am also looking into frank&#39;s pyode script. Is there something in it,<br>that can do that?<br>marius.<br><br>_______________________________________________<br><a href="mailto:PD-list@iem.at">
PD-list@iem.at</a> mailing list<br>UNSUBSCRIBE and account-management -&gt; <a href="http://lists.puredata.info/listinfo/pd-list">http://lists.puredata.info/listinfo/pd-list</a></blockquote></div>