-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathsticks-stones-intersect.js
More file actions
129 lines (106 loc) · 3.03 KB
/
Copy pathsticks-stones-intersect.js
File metadata and controls
129 lines (106 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* MoMath Math Square Behavior
*
* Title: Sticks Stones Intersect
* Description: Draw a circle and line? Possibly development testing for sticks-stones
* Scheduler ID:
* Framework: THREE
* Author: ?
* Created: ?
* Status: unknown
*/
import THREEContext from 'threectx';
import * as THREE from 'three';
import * as Display from 'display';
import {Ghost} from 'floor';
var context;
var circle = new THREE.Mesh(new THREE.SphereGeometry(100,100,100), new THREE.MeshBasicMaterial({color:0xdddddd, opacity:0.4}))
var line;
function init(container) {
context = new THREEContext(container, true);
container.addEventListener( 'mousemove', onDocumentMouseMove, false );
circle.position.set(Display.width/2, Display.height/2, 0)
context.scene.add(circle);
}
function makeNewGhosts(){
for(var i=0;i<5;i++){
var g = new Ghost(undefined, 0.0002);
ghosts.push(g);
}
}
function isIntersecting(a,b){
var bX = b.x;
var bY = b.y;
var aX = a.x;
var aY = a.y;
var cX = circle.position.x;
var cY = circle.position.y;
var R = 100;
var dX = bX - aX;
var dY = bY - aY;
if ((dX == 0) && (dY == 0)){
// A and B are the same points, no way to calculate intersection
return false;
}
var dl = (dX * dX + dY * dY);
var t = ((cX - aX) * dX + (cY - aY) * dY) / dl;
// point on a line nearest to circle center
var nearestX = aX + t * dX;
var nearestY = aY + t * dY;
var nearest = new THREE.Vector3(nearestX,nearestY,0);
var dist = circle.position.distanceTo(nearest);
if (dist == R) {
// line segment touches circle; one intersection point
iX = nearestX;
iY = nearestY;
if (t < 0 || t > 1) {
return true;
// intersection point is not actually within line segment
}
} else if (dist < R) {
// two possible intersection points
var dt = Math.sqrt(R * R - dist * dist) / Math.sqrt(dl);
// intersection point nearest to A
var t1 = t - dt;
//i1X = aX + t1 * dX;
//i1Y = aY + t1 * dY;
if (t1 < 0 || t1 > 1){
// intersection point is not actually within line segment
return false;
}
// intersection point farthest from A
var t2 = t + dt;
//i2X = aX + t2 * dX;
//i2Y = aY + t2 * dY;
if (t2 < 0 || t2 > 1) {
// intersection point is not actually within line segment
return false;
}
return true;
} else {
return false;
// no intersection
}
}
//
function onDocumentMouseMove(event) {
var st = new THREE.Vector3(event.offsetX, event.offsetY,0);
var end = new THREE.Vector3(200, 40,0);
var geo = new THREE.Geometry();
geo.vertices.push(st);
geo.vertices.push(end);
if(line != undefined) context.scene.remove(line);
line = new THREE.Line(geo, new THREE.LineBasicMaterial({color:(isIntersecting(st,end)? 0xff0000: 0xffffff), linewidth:2}));
context.scene.add(line);
}
//
function animate() {
context.render();
}
export const behavior = {
title: "Sticks Stones Intersect",
frameRate: 'animate',
maxUsers: null,
init: init,
render: animate
};
export default behavior;