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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="assets/stylesheets/css.css">
<style type="text/css">
body > div {
float: left;
}
</style>
</head>
<body>
<div id="map"></div>
<div id="info">
<span id="intersects"></span>
</div>
</body>
<script type="text/javascript" src="assets/javascripts/util.js"></script>
<script type="text/javascript">
function vec2(a,b){
this.a = a
this.b = b
}
vec2.prototype.magnitude = function(){
return this.b-this.a
}
vec2.prototype.length = function(){
return abs(this.b-this.a)
}
vec2.prototype.clone = function(){
return new vec2(this.a, this.b)
}
vec2.prototype.normalize = function(){
if (this.b < this.a) {
this.a = this.a ^ this.b
this.b = this.a ^ this.b
this.a = this.a ^ this.b
}
return this
}
vec2.prototype.add = function(n){
this.a += n
this.b += n
}
vec2.prototype.sub = function(n){
this.a -= n
this.b -= n
}
vec2.prototype.mul = function(n){
this.a *= n
this.b *= n
}
vec2.prototype.div = function(n){
this.a /= n
this.b /= n
}
vec2.prototype.contains = function(n){
return this.a <= n && n <= this.b
}
vec2.prototype.intersects = function(v){
return (this.a <= v.a && this.b > v.a) ||
(v.a <= this.a && v.b > this.a)
}
vec2.prototype.union = function(v){
if (this.intersects(v)) {
return new vec2( min(this.a,v.a), max(this.b, v.b) )
}
}
vec2.prototype.intersection = function(v){
if (this.intersects(v)) {
return new vec2( max(this.a,v.a), min(this.b, v.b) )
}
}
function rect (x0,y0,x1,y1){
if (x0 instanceof vec2) {
this.x = x0
this.y = y0
}
else if (x1 === undefined) {
this.x = new vec2(x0,x0)
this.y = new vec2(y0,y0)
}
else {
this.x = new vec2(x0,x1)
this.y = new vec2(y0,y1)
}
this.translation = new vec2(0,0)
}
rect.prototype.clone = function(){
return new rect( this.x.clone(), this.y.clone() )
}
rect.prototype.normalize = function(){
this.x.normalize().add(this.translation.a)
this.y.normalize().add(this.translation.b)
this.translation.a = this.translation.b = 0
return this
}
rect.prototype.contains = function(x,y){
return this.x.contains(x) && this.y.contains(y)
}
rect.prototype.intersects = function(r){
return this.x.intersects(r.x) && this.y.intersects(r.y)
}
rect.prototype.width = function(){ return this.x.length() }
rect.prototype.height = function(){ return this.y.length() }
rect.prototype.fill = function(){
ctx.fillRect(this.x.a + this.translation.a, this.y.a + this.translation.b, this.x.length(), this.y.length())
}
rect.prototype.stroke = function(){
ctx.beginPath()
ctx.moveTo(this.x.a, this.y.a)
ctx.lineTo(this.x.b, this.y.b)
ctx.stroke()
}
var canvas = document.createElement("canvas")
var ctx = canvas.getContext("2d")
var w = canvas.width = 500
var h = canvas.height = 500
document.body.appendChild(canvas)
var rects = [
new rect(100,100, 300,300),
new rect(200,200, 400,400),
]
var creating = false, dragging = false
var mouse = new rect(0,0,0,0)
canvas.addEventListener("mousedown", function(e){
var x = e.pageX, y = e.pageY
mouse = new rect (x,y)
var intersects = rects.filter(function(r){ return r.contains(x,y) })
console.log(intersects)
if (intersects.length){
dragging = intersects[0]
}
else {
creating = true
}
})
canvas.addEventListener("mousemove", function(e){
mouse.x.b = e.pageX
mouse.y.b = e.pageY
if (dragging) {
dragging.translation.a = mouse.x.magnitude()
dragging.translation.b = mouse.y.magnitude()
}
else if (creating) {
mouse.x.b = e.pageX
mouse.y.b = e.pageY
}
else {
mouse.x.a = mouse.x.b
mouse.y.a = mouse.y.b
}
})
document.addEventListener("mouseup", function(e){
if (creating) {
if (mouse.height() != 0 && mouse.width() != 0) {
rects.push(mouse.normalize())
}
}
if (dragging) {
dragging.normalize()
}
mouse = new rect(e.pageX, e.pageY)
creating = dragging = false
})
function animate(){
requestAnimationFrame(animate)
ctx.fillStyle = "#fff"
ctx.fillRect(0,0,w,h)
solve_rects()
draw_ruler()
draw_rects()
draw_mouse()
}
animate()
function draw_ruler(){
ctx.strokeStyle = "rgba(80,80,80,0.5)"
ctx.lineWidth = 1
var len = 5
for (var i = 0.5; i < w; i += 10) {
line(i, 0, i, len)
line(0, i, len, i)
}
}
function line (x,y,a,b){
ctx.beginPath()
ctx.moveTo(x,y)
ctx.lineTo(a,b)
ctx.stroke()
}
function solve_rects(){
var x_intervals = []
var y_intervals = []
for (var i = 0; i < rects.length; i++) {
}
}
function draw_regions(){
}
function draw_rects(){
ctx.strokeStyle = "rgba(80,80,80,0.5)"
ctx.lineWidth = 1
// ctx.setLineDash([randint(5)+2,randint(2)+2]);
ctx.setLineDash([1,1]);
for (var i = 0; i < rects.length; i++) {
ctx.fillStyle = "rgba(0,200,220,0.5)"
rects[i].fill()
// line(rect.x, 0, rect.x, rect.y)
// line(0, rect.y, rect.x, rect.y)
}
}
function draw_mouse(){
ctx.fillStyle = "rgba(255,0,0,0.4)";
ctx.beginPath();
ctx.arc(mouse.x.b, mouse.y.b, 5, 0, 2*Math.PI, false);
ctx.fill();
if (mouse.width() != 0 && mouse.height() != 0) {
if (dragging) {
mouse.stroke()
}
else {
ctx.fillStyle = "rgba(255,255,0,0.5)"
mouse.clone().normalize().fill()
}
}
}
</script>
</html>
|