Published
Edited
Dec 12, 2020
1 fork
3 stars
Insert cell
Insert cell
Insert cell
Insert cell
function hitter(o) {
let ray = [
{x: o.x, y: o.y},
{x: c.x, y: c.y}
]
let hit
if(hit = intersect(ray, left)) { hit.side = "left"; return hit }
if(hit = intersect(ray, top)) { hit.side = "top"; return hit }
if(hit = intersect(ray, right)) { hit.side = "right"; return hit }
if(hit = intersect(ray, bot)) { hit.side = "bottom"; return hit }
}
Insert cell
function intersect(a, b) {
let da = {x: a[1].x - a[0].x, y: a[1].y - a[0].y }
let db = {x: b[1].x - b[0].x, y: b[1].y - b[0].y }
let s = (-da.y * (a[0].x - b[0].x) + da.x * (a[0].y - b[0].y)) / (-db.x * da.y + da.x * db.y);
let t = ( db.x * (a[0].y - b[0].y) - db.y * (a[0].x - b[0].x)) / (-db.x * da.y + da.x * db.y);
let hit = {x: 0, y: 0 }
console.log("s,t", s, t)
if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
{
// Collision detected
hit.x = a[0].x + (t * da.x);
hit.y = a[0].y + (t * da.y);
hit.t = t
return hit
}
}
Insert cell
left = [
{x: c.x - rectWidth/2, y: c.y - rectWidth/2},
{x: c.x - rectWidth/2, y: c.y + rectWidth/2},
]
Insert cell
Insert cell
top = [
{x: c.x - rectWidth/2, y: c.y - rectWidth/2},
{x: c.x + rectWidth/2, y: c.y - rectWidth/2}
]
Insert cell
bot = [
{x: c.x - rectWidth/2, y: c.y + rectWidth/2},
{x: c.x + rectWidth/2, y: c.y + rectWidth/2}
]
Insert cell
c = { return { x: width/2, y: height/2 } }
Insert cell
rectWidth = 150
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more