I have a two 2D points u = (ux, uy)
and v = (vx, vy)
that define a line segment.
Additionally I have an angle θ
that is defined relative to the coordinate system (angle to x-axis), indicating the directing of a moving particle.
Is there a simple way to find the angle of reflection resulting (again, relative to the coordinate system) from that particle bouncing off the line segment?
So far I have found the angle of the line segment θuv = numpy.arctan2(vy-uy, vx-ux)
, taken the difference Δθ = θuv - θ
and set the resulting angle to θ_reflected = θ - 2*Δθ
.
Sometimes, this seems to work, but other times it's completely off.
Segment has length
leng = hypot(vy-uy, vx-ux)
and unit direction vector (perhaps in numpy there is ready function like normalized
)
dx = (vx-ux) / leng
dy = (vy-uy) / leng
Unit normal to segment
nx = - dy
ny = dx
particle direction vector is
px = cos(θ)
py = sin(θ)
Reflected vector
dott = dot(p, n) = px * nx + py * ny
rx = px - 2 * dott * nx
ry = py - 2 * dott * ny
If you need angle
θ_reflected = atan2(ry, rx)
but sometimes particle direction vector components are more useful