Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/examples/old/quadrilaterals/js/lesson.js
blob: 03cbacef1419604af22a1bd1522baa037ddd451c (plain)
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
$(document).ready(function(){
  
	var canvasDrawing = $("#canvasDrawing")[0];
	var ctxDrawing    = canvasDrawing.getContext("2d"); 
	
	var canvas    =$("#canvas")[0];
	var ctx       = canvas.getContext("2d"); 
	
	var canvasTmp = $("#canvasTmp")[0]; 
	var ctxTmp    = canvasTmp.getContext("2d"); 
	ctxTmp.lineWidth = 3;
	ctxTmp.lineCap = 'round';
	
	var points=[]; //points of the quadrilateral
	
	function reset() {
		ctxDrawing.clearRect(0, 0, canvas.width, canvas.height);  
		ctxTmp.clearRect(0, 0, canvas.width, canvas.height);
		started=false;
		points=[];
	}
	
	var buttons= {
		next:{ x:510, y:135, width:30, height:30},
		prev:{ x:330, y:135, width:30, height:30}
		};
	
	//f of functions, a "place" to put functions ;)
	var f={
		drawCircle: function ( context, x, y, size){
			context.save();
			//context.translate(x,y);
			context.beginPath();
			context.fillStyle = "#cccccc"
			context.strokeStyle = "#000000" 
			context.lineWidth = 1;
			context.arc( x ,y, size, 0, Math.PI*2, true);
			context.closePath();
			context.fill();
			context.stroke();
			context.restore();
		},
		equals: function (x, y) {
			if ( x.lenght != y.length ) return false;
			for(var propertyName in x) {
				if(x[propertyName] !== y[propertyName]) {
					return false;
				}
			}
			return true;
		}
	};
	var geometry = {
		// 2D cross product.
		// return a positive value, if OAB makes a counter-clockwise turn,
		// negative for clockwise turn, and zero if the points are collinear.
		cross: function ( o, a, b ) {
			return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
		}
	};
	
	//var imgBg = document.getElementById("bg"); 	//creates the image element for background
	var imgErase = document.getElementById("btnErase");;	//creates the image element for erase button
	var imgPrev = document.getElementById("imgPrev");//creates the image element for previous button
	var imgNext = document.getElementById("imgNext");//creates the image element for next button
	
	imgNext.onmouseup = function()
	{
		$.jGrowl("next pressed");
	};
	
	imgPrev.onmouseup = function()
	{
		$.jGrowl("previous pressed");
	};
	
	imgErase.onmouseup = function()
	{
		reset();
	}
	//necessary stuff to do test the figure
	var match = false;
	function sortPoint( a, b) {
		if (a.x == b.x) return (a.y < b.y);
		return (a.x < b.x);	
	}
	function checkQuadrilateral( points ) {
		started = false;
		if ( f.equals (points[0], points[4]) === false ) {
			return false;
		} 
		points.push(points[ 1 ]); //add the second point (pos 1) to create a loop 0-1, 1-2, 2-3, 3-4, 4-1
		//slope
		var m, m0 = undefined;
		for ( var i=0; i<5; i++ ) {
			m  = (points[ (i+1)%6 ].y - points[ i ].y) / (points[ (i+1)%6 ].x - points[ i ].x);	
			if ( m === m0) {				
				return false;
			}
			m0 = m;
		}
		//clockwise or anti-clockwise ?? choose it
		var dir = geometry.cross( points[0], points[1], points[2])<=0 ? 0: 1; // get the direction
		for ( var i=2; i<5; i++ ) {
			if ( (geometry.cross( points[i-2], points[i-1], points[i]) <= 0 ?0:1) != dir) {
				return false;
			}
		}
		return true;
		
	}
		
		
		//drawing area
		ctx.fillStyle = "rgba( 111, 226, 245, 0.7)"; 
		ctx.strokeStyle = "#B74000" 
		ctx.lineWidth = 3;
		ctx.fillRect( 10, 70, 310, 150 ); 
		ctx.strokeRect( 10, 70, 310, 150 ); 
		
		//settings for the grid
		ctx.strokeStyle = "rgba( 111, 226, 245, 1)" 
		ctx.lineWidth = 1;
		//draw the row lines
		for (var i=0; i<5; i ++) {
			ctx.beginPath();
			ctx.moveTo( 330, 70 + i*15 );
			ctx.lineTo( 540, 70 + i*15 );
			ctx.closePath();
			ctx.stroke()
		}
		//draw the column lines
		for (var i=0; i<8; i ++) {
			ctx.beginPath();
			ctx.moveTo( 330 + i*30, 70  );
			ctx.lineTo( 330 + i*30, 130 );
			ctx.closePath();
			ctx.stroke()
		}
		
		
		//the dotButton definition
		var dotButton = function( valx, valy, size){
			var obj={
				x:valx, y:valy, width:size, height:size, 
				mouseup:function( ev ) {
					var p = { x:valx, y:valy };
					if ( points.length > 0 && f.equals( points[ points.length -1 ], p ) ) return;
						
					points.push( p );
				
					//clear the current line in order to adjust it
					ctxTmp.clearRect(0, 0, canvas.width, canvas.height);
					ctxTmp.beginPath();
					if ( points.length === 1) {
						ctxTmp.moveTo( valx + 7,  valy + 7 );
					} else {
						ctxTmp.moveTo(mouse.x0, mouse.y0 );
					}
					ctxTmp.lineTo(valx + 7,  valy + 7);
					ctxTmp.stroke();
					ctxTmp.closePath();
					
					
					//do updating
					ctxDrawing.drawImage(canvasTmp, 0, 0);
					
					//+7 hack, start point for the next line if any
					mouse.x0 = valx + 7;
					mouse.y0 = valy + 7;
					
					if (points.length === 5) {
						if (checkQuadrilateral( points ) === true) {
							$.jGrowl("good! ");
						}else {
							$.jGrowl("that is not a quadrilateral ");
						}
						reset();
					}
				
				}, 
				mousedown:function( ev ) { 
					ctxTmp.moveTo( mouse.x0, mouse.y0 );
					//set the default beahavior
					if (started !== true) {
						mouse.x0 = mouse.x;
						mouse.y0 = mouse.y;
						started = true;
					}
				},
				mouseover:function( ev ) { } 
			}
			return obj;
		};
	
		//draw the circles
		//buttons["dot"]=dotButton(100-25, 100-25, 50);
		//f.drawCircle( ctx, 100, 100, 25);
		for (var i=0; i<5; i++) {
			for (var j=0; j<8; j++) {
				//tricky way.. 
				buttons["dot"+i+""+j]=dotButton(10 + j*40 +15 -7, 70 + i*30 +15 -7, 14);
				f.drawCircle( ctx, 10 + j*40 +15, 70 + i*30 +15, 7);
			}
		}
		
    
	
	//clipping path, this is the drawing region
	ctxTmp.beginPath();
	ctxTmp.moveTo(10,   70);
	ctxTmp.lineTo(320,  70);
	ctxTmp.lineTo(320, 220);
	ctxTmp.lineTo(10,  220);
	ctxTmp.lineTo(10,   70);
	ctx.closePath();
	ctxTmp.clip();
	
	
	
	function distance2 ( x1, x2, y1, y2){
		return   (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) ; 
	}
	var mouse= {x:0, y:0, x0:0, y0:0, listeners:[] };
	var started=false,counter=0;

	//drawing object	
	
	var drawing={};
	drawing.mouseup =function ( ev ) {
		//see dotButton.mouseup	
	}
	drawing.mousedown = function (ev) {
		//see dotButton.mousedown
	}
	drawing.mousemove = function (ev) {
		if (started) {
			
			ctxTmp.clearRect(0, 0, canvas.width, canvas.height);
			ctxTmp.beginPath();
			
			ctxTmp.moveTo(mouse.x0, mouse.y0);
			ctxTmp.lineTo(mouse.x,  mouse.y );
			ctxTmp.stroke();
			ctxTmp.closePath();
		}
	}
	
	function mouseHandler( ev ){
		
		mouse.x = ev.layerX ;
		mouse.y = ev.layerY;
		
		//calls the listeners if any
		$.each( mouse.listeners, function( i, val ){
			mouse.listeners[i][ev.type]( ev );
		});
		
		
		//debug="";
		$.each(buttons, function( i, val ){
			d = distance2(mouse.x, buttons[i].x + buttons[i].width/2, mouse.y, buttons[i].y + buttons[i].height/2);
			mouse.minDist=(buttons[i].width/2) * (buttons[i].height/2);
			
			if ( d <= mouse.minDist ){
				if ( ev.type==="mousemove"){
					buttons[i]["mouseover"]( ev );
				}
				else {
					buttons[i][ev.type]( ev );
				}
				
				//debug+= ev.type + " fire!";
			}
			
		});
		//$("#debug").html( debug );
	}
	
	//Karma handler (need to work around mouse out)
	canvasTmp.addEventListener('mouseup',   mouseHandler, false);
	canvasTmp.addEventListener('mousedown', mouseHandler, false);
	canvasTmp.addEventListener('mousemove', mouseHandler, false);
	
	//wiring (Karma stuff)
	//mouseHandler will call this listener :)
	mouse.listeners.push( drawing );
		
});