|
| 1 | +/*global c*/ |
| 2 | +/** |
| 3 | + * [c1, c2, c3, c4] for corners starting from top left clockwise |
| 4 | + * [m1, m2, m3, m4] for midpoints starting from top clockwise |
| 5 | + */ |
| 6 | +var expressions = [ |
| 7 | + // midpoints constrained in corners |
| 8 | + '(c1x + c2x) / 2 == m1x', '(c1y + c2y) / 2 == m1y', |
| 9 | + '(c2x + c3x) / 2 == m2x', '(c2y + c3y) / 2 == m2y', |
| 10 | + '(c4x + c3x) / 2 == m3x', '(c4y + c3y) / 2 == m3y', |
| 11 | + '(c1x + c4x) / 2 == m4x', '(c1y + c4y) / 2 == m4y', |
| 12 | + |
| 13 | + // spaces between points |
| 14 | + 'c1x + 20 <= c2x', 'c1x + 20 <= c3x', |
| 15 | + 'c4x + 20 <= c2x', 'c4x + 20 <= c3x', |
| 16 | + 'c1y + 20 <= c3y', 'c1y + 20 <= c4y', |
| 17 | + 'c2y + 20 <= c3y', 'c2y + 20 <= c4y', |
| 18 | + |
| 19 | + // contained inside canvas |
| 20 | + 'c1x >= 0', 'c2x >= 0', 'c3x >= 0', 'c4x >= 0', |
| 21 | + 'c1y >= 0', 'c2y >= 0', 'c3y >= 0', 'c4y >= 0', |
| 22 | + 'c1x <= 800', 'c2x <= 800', 'c3x <= 800', 'c4x <= 800', |
| 23 | + 'c1y <= 600', 'c2y <= 600', 'c3y <= 600', 'c4y <= 600', |
| 24 | +].join(';'); |
| 25 | + |
| 26 | +var cOut = c(expressions); |
| 27 | +var solver = c('solver'); |
| 28 | + |
| 29 | + |
| 30 | +/** |
| 31 | + * Point - holds c.Variable coordinate representing a point |
| 32 | + */ |
| 33 | +function Point(x, y) { |
| 34 | + this.x = x; |
| 35 | + this.y = y; |
| 36 | + this.size = 15; |
| 37 | +} |
| 38 | + |
| 39 | +Point.prototype = { |
| 40 | + // checks if a given coordinate is inside the box representing the point |
| 41 | + contains: function(x, y) { |
| 42 | + return (Math.abs(x - this.x.value) <= this.size/2 && |
| 43 | + Math.abs(y - this.y.value) <= this.size/2); |
| 44 | + }, |
| 45 | + |
| 46 | + // draws a box representing the point |
| 47 | + draw: function(ctx) { |
| 48 | + var z = this.size; |
| 49 | + ctx.strokeRect(this.x.value - z/2, this.y.value - z/2, z, z); |
| 50 | + }, |
| 51 | + |
| 52 | + // sets stay value on the point |
| 53 | + stay: function(x, y) { |
| 54 | + this.x.value = x; |
| 55 | + this.y.value = y; |
| 56 | + solver.addStay(this.x).addStay(this.y); |
| 57 | + }, |
| 58 | + |
| 59 | + // makes point coordinate variables editable |
| 60 | + edit: function() { |
| 61 | + return solver.addEditVar(this.x).addEditVar(this.y); |
| 62 | + }, |
| 63 | + |
| 64 | + // suggests coordinate values for the point |
| 65 | + suggest: function(x, y) { |
| 66 | + return solver.suggestValue(this.x, x).suggestValue(this.y, y); |
| 67 | + } |
| 68 | +}; |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +/** |
| 73 | + * Application |
| 74 | + */ |
| 75 | +var App = { |
| 76 | + init: function() { |
| 77 | + this.canvas = document.getElementById('c'); |
| 78 | + this.cwidth = this.canvas.width; |
| 79 | + this.cheight = this.canvas.height; |
| 80 | + this._ctx = this.canvas.getContext('2d'); |
| 81 | + this._dragPoint = null; |
| 82 | + |
| 83 | + // populating corners, midpoints |
| 84 | + var ps = this.points = []; |
| 85 | + var cs = this.corners = []; |
| 86 | + var ms = this.midpoints = []; |
| 87 | + var point; |
| 88 | + for(var i=1; i<=4; i++) { |
| 89 | + point = new Point(c(`c${i}x`), c(`c${i}y`)); |
| 90 | + cs.push(point); |
| 91 | + ps.push(point); |
| 92 | + |
| 93 | + point = new Point(c(`m${i}x`), c(`m${i}y`)); |
| 94 | + ms.push(point); |
| 95 | + ps.push(point); |
| 96 | + } |
| 97 | + |
| 98 | + // set initial position |
| 99 | + cs[0].stay(100, 100); |
| 100 | + cs[1].stay(400, 100); |
| 101 | + cs[2].stay(400, 400); |
| 102 | + cs[3].stay(100, 400); |
| 103 | + ms[0].stay(250, 100); |
| 104 | + ms[1].stay(400, 250); |
| 105 | + ms[2].stay(250, 400); |
| 106 | + ms[3].stay(100, 250); |
| 107 | + |
| 108 | + this.draw(); |
| 109 | + |
| 110 | + this._bindEvents(); |
| 111 | + }, |
| 112 | + |
| 113 | + draw: function() { |
| 114 | + var g = this._ctx; |
| 115 | + g.clearRect(0, 0, this.cwidth, this.cheight); |
| 116 | + g.strokeStyle = 'black'; |
| 117 | + |
| 118 | + this.points.forEach(function(point) { point.draw(g); }); |
| 119 | + |
| 120 | + this._drawLine(this.midpoints); |
| 121 | + this._drawLine(this.corners); |
| 122 | + }, |
| 123 | + |
| 124 | + _drawLine: function(points) { |
| 125 | + var g = this._ctx; |
| 126 | + |
| 127 | + g.beginPath(); |
| 128 | + g.moveTo(points[0].x.value, points[0].y.value); |
| 129 | + g.lineTo(points[1].x.value, points[1].y.value); |
| 130 | + g.lineTo(points[2].x.value, points[2].y.value); |
| 131 | + g.lineTo(points[3].x.value, points[3].y.value); |
| 132 | + g.closePath(); |
| 133 | + g.stroke(); |
| 134 | + }, |
| 135 | + |
| 136 | + _bindEvents: function() { |
| 137 | + this.canvas.addEventListener('mousedown', this._mousedown.bind(this)); |
| 138 | + document.body.addEventListener('mousemove', this._mousemove.bind(this)); |
| 139 | + document.body.addEventListener('mouseup', this._mouseup.bind(this)); |
| 140 | + }, |
| 141 | + |
| 142 | + _mousedown: function(ev) { |
| 143 | + var x = ev.pageX - this.canvas.offsetLeft; |
| 144 | + var y = ev.pageY - this.canvas.offsetTop; |
| 145 | + |
| 146 | + for(var i=0; i<this.points.length; i++) { |
| 147 | + if(this.points[i].contains(x, y)) { |
| 148 | + this._dragPoint = this.points[i]; |
| 149 | + this._dragPoint.edit().beginEdit(); |
| 150 | + document.body.style.cursor = 'move'; |
| 151 | + } |
| 152 | + } |
| 153 | + }, |
| 154 | + |
| 155 | + _mousemove: function(ev) { |
| 156 | + if(!this._dragPoint) return; |
| 157 | + |
| 158 | + var x = ev.pageX - this.canvas.offsetLeft; |
| 159 | + var y = ev.pageY - this.canvas.offsetTop; |
| 160 | + this._dragPoint.suggest(x, y).resolve(); |
| 161 | + this.draw(); |
| 162 | + }, |
| 163 | + |
| 164 | + _mouseup: function(ev) { |
| 165 | + if(this._dragPoint) { |
| 166 | + solver.endEdit(); |
| 167 | + document.body.style.cursor = ''; |
| 168 | + } |
| 169 | + this._dragPoint = null; |
| 170 | + } |
| 171 | +}; |
| 172 | +App.init(); |
0 commit comments