Skip to main content
100%

Calculating the relative angle difference between two vectors

✓ Published0🌍 Public
TThanaporn-sk
Last edited May 15, 2017
Created on May 15, 2017

Two draggable vectors extend from a central origin, and the smaller angle between them is highlighted as a filled arc. A label reports the angle in degrees and whether it is "port" or "starboard". The interaction uses D3 v4's `d3.drag` to update vector positions, while the arc is drawn with `d3.arc` and `Math.atan2` computes the relative angle using the provided formula. The visualization renders as SVG with an arrowhead marker, and the arc color shifts between cyan and magenta based on the angle's sign.

AI-generated description

Here are the math-y bits, courtesy of @ch402:

var a2 = Math.atan2(source.y, source.x);
var a1 = Math.atan2(compare.y, compare.x);
var sign = a1 > a2 ? 1 : -1;
var angle = a1 - a2;
var K = -sign * Math.PI * 2;
var angle = (Math.abs(K + angle) < Math.abs(angle))? K + angle : angle;

forked from <a href='http://bl.ocks.org/shancarter/'>shancarter</a>'s block: <a href='http://bl.ocks.org/shancarter/1034db3e675f2d3814e6006cf31dbfdc'>Calculating the relative angle difference between two vectors</a>

mit Licensed

Similar vizzes