@IsoWalle My funny dirty code is here:
Content.makeFrontInterface(400, 400);
// distance squared
inline function distance2(p0, p1)
{
local dx = p1.x - p0.x;
local dy = p1.y - p0.y;
return dx * dx + dy * dy;
}
// angle between two points
inline function angleBetween(p0, p1)
{
local dx = p1.x - p0.x;
local dy = p1.y - p0.y;
// convert atan() to atan2()
local a = Math.atan(dy / dx);
if(dx > 0)
{
if(dy < 0)
a += Math.PI;
else
a -= Math.PI;
}
return a;
}
const var points = [
{x: 200, y: 102},
{x: 100, y: 275},
{x: 300, y: 275}
];
const var radius = 200;
const var radius2 = radius * radius;
reg dragger = {x: 200, y: 217};
reg draggerRadius = 10;
reg values = [0.0, 0.0, 0.0];
reg valueIndex = 0;
const var canvas = Content.addPanel("canvas", 0, 0);
Content.setPropertiesFromJSON("canvas", {
"width": 400,
"height": 400,
"allowCallbacks": "Clicks, Hover & Dragging"
});
canvas.setPaintRoutine(function(g)
{
// draw guides
g.setColour(0x40FFFFFF);
for(p in points)
{
g.drawEllipse([p.x - radius, p.y - radius, radius * 2, radius * 2], 1);
g.fillEllipse([p.x - 2, p.y - 2, 4, 4]);
}
// draw dragger
g.setColour(0xFFFFFFFF);
g.fillEllipse([dragger.x - draggerRadius, dragger.y - draggerRadius, draggerRadius * 2, draggerRadius * 2]);
});
canvas.setMouseCallback(function(event)
{
if(event.drag)
{
dragger.x = event.x;
dragger.y = event.y;
// limit the drag area
for(p in points)
{
if(distance2(dragger, p) >= radius2)
{
var a = angleBetween(dragger, p);
dragger.x = p.x + Math.cos(a) * radius;
dragger.y = p.y + Math.sin(a) * radius;
}
}
canvas.repaint();
// calculate distance between points
valueIndex = 0;
for(p in points)
{
var d2 = distance2(dragger, p);
values[valueIndex] = Math.round((200.0 - Math.sqrt(d2)) / 2);
valueIndex++;
}
Console.print(values.join(", "));
}
});