Last active
August 29, 2015 14:14
-
-
Save blewert/e95f581ce3ec3b4cfffd to your computer and use it in GitHub Desktop.
Proof of concept for a triad UI component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var p = { | |
x: 186, | |
y: 120, | |
w: 6 | |
}; | |
var triad = { | |
first: { | |
x: 292, | |
y: 207 | |
}, | |
second: { | |
x: 102, | |
y: 205 | |
}, | |
third: { | |
x: 198, | |
y: 50 | |
} | |
}; | |
var checkIfInside = function(p, p1, p2, p3) { | |
//var p1 = triad.first, p2 = triad.second, p3 = triad.third; | |
//Can be calculated only once: | |
var denom = (p1.y - p3.y) * (p1.x - p3.x) + (p3.x - p2.x) * (p1.y - p3.y); | |
//These will be continously calculated | |
var pResults = { | |
x: (p.x - p3.x), | |
y: (p.y - p3.y) | |
}; | |
//The rest of these can be cached if the triangle never moves | |
var a = ((p2.y - p3.y) * pResults.x + (p3.x - p2.x) * pResults.y) / denom; | |
var b = ((p3.y - p1.y) * pResults.x + (p1.x - p3.x) * pResults.y) / denom; | |
var c = 1.0 - a - b; | |
//Test whether or not a, b and c are between 0 and 1 inclusive. | |
var aNormalized = (0 <= a && a <= 1); | |
var bNormalized = (0 <= b && b <= 1); | |
var cNormalized = (0 <= c && c <= 1); | |
if(aNormalized && bNormalized && cNormalized) | |
{ | |
return { x : a, y : b, z : c }; | |
} | |
else | |
{ | |
return null; | |
} | |
}; | |
line(triad.first.x, triad.first.y, triad.second.x, triad.second.y); | |
line(triad.second.x, triad.second.y, triad.third.x, triad.third.y); | |
line(triad.third.x, triad.third.y, triad.first.x, triad.first.y); | |
ellipse(p.x, p.y, p.w, p.w); | |
mouseDragged = function() { | |
background(255, 255, 255); | |
p.x = mouseX; | |
p.y = mouseY; | |
line(triad.first.x, triad.first.y, triad.second.x, triad.second.y); | |
line(triad.second.x, triad.second.y, triad.third.x, triad.third.y); | |
line(triad.third.x, triad.third.y, triad.first.x, triad.first.y); | |
ellipse(p.x, p.y, p.w, p.w); | |
var insideData = checkIfInside(p, triad.first, triad.second, triad.third); | |
if (insideData !== null) | |
{ | |
println("inside " + insideData.x + ", " + insideData.y + ", " + insideData.z); | |
} | |
else | |
{ | |
println("outside"); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment