Okay, let's break down the derivation of the parking rectangle algorithm in an intuitive, step-by-step manner. We'll focus on the core logic and avoid getting bogged down in heavy matrix notation.
The Goal:
Imagine you have a street segment defined by its center point and direction. You want to draw two parking rectangles, one on each side of the street, with a specified width, height, and offset from the street's center.
The Challenge:
The world isn't flat! We're dealing with latitude and longitude on a (roughly) spherical Earth. So, simply adding distances in meters to coordinates won't work accurately, especially as we move further away from the equator.
The Solution: A Series of Transformations
We'll achieve our goal by performing a sequence of transformations, essentially moving between a simplified "flat-Earth" model for calculations and the real-world geographic coordinates.
1. The "Flat-Earth" Playground: Local Cartesian Coordinates
2. Building the Template Rectangle (Before Scaling and Rotation)
W and height of H centered at the origin of our flat plane. The vertices of the rectangle are given by the matrix V_base.K parameter is the distance from the street center line to the nearer edge of the rectangle.V_base = [[-W/2, K],
[-W/2, K+H],
[W/2, K+H],
[W/2, K]]
-W/2 and W/2 define the left and right edges of the rectangle respectively.K shifts the rectangle upwards (northwards) by K meters.K + H defines the top edge of the rectangle.K meters away from the origin.3. Rotating the Rectangle to Align with the Street
Street orientation (θ): We know the direction the street is pointing, measured as an angle θ clockwise from true north.
Rotation matrix (R(θ)): We use a standard rotation matrix to rotate our template rectangle by this angle θ counter-clockwise in our flat plane:
R(θ) = [[cos(θ), -sin(θ)],
[sin(θ), cos(θ)]]
Applying the rotation: Multiply the V_base matrix by the transpose of the rotation matrix (R(θ)^T) to get the rotated vertices (V_rotated). The transpose is necessary due to how matrix multiplication works with row and column vectors. We are essentially rotating each vertex of the rectangle.
V_rotated = V_base * R(θ)^T4. Stepping Back into the Real World: Geographic Normalization
Meters to degrees: Now we need to convert our rotated rectangle's coordinates from meters (in our flat plane) to latitude and longitude (degrees).
Geographic Normalization Matrix (G): This matrix handles the conversion. It's based on the idea that a meter corresponds to a different change in latitude/longitude depending on where you are on Earth.
G = [[1/g_lat, 0],
[0, 1/g_lon]]
where:
g_lat = R_lat * (π / 180)
g_lon = R_lon * cos(v_lat * π / 180) * (π / 180)
R_lat and R_lon are the Earth's radii of curvature at the street's latitude (v_lat). These values are calculated for an accurate ellipsoidal model of the Earth.g_lat tells you how many meters correspond to one degree of latitude change.g_lon tells you how many meters correspond to one degree of longitude change. The cos(v_lat) term accounts for the fact that longitude lines converge as you move towards the poles.(π / 180) converts degrees to radians, as required by the trigonometric functions and the definition of the radius of curvature.Applying the normalization: Multiply V_rotated by the transpose of the G matrix (G^T) to get the geographically normalized vertices (V_geo). Again, the transpose is needed for correct matrix multiplication.
V_geo = V_rotated * G^T5. Placing the Rectangle on the Map
v to each vertex in V_geo. This places the rectangle correctly on the map.R₁ = V_geo + v (this is element-wise addition).6. Creating the Symmetric Rectangle on the Other Side of the Street
V_rotated vertices by 180 degrees before we convert them to geographic coordinates. Rotating by 180 degrees is the same as multiplying by -1.G^T) and translation (v) as before.R₂ = -V_rotated * G^T + vIn a Nutshell
Important Notes
This intuitive explanation should give you a good grasp of the underlying logic of the parking rectangle algorithm. By understanding each step, you can better appreciate the algorithm's strengths, limitations, and how to use it effectively.