Created
January 25, 2012 20:05
-
-
Save stravid/1678353 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
canvas = document.getElementById 'canvas' | |
canvas.width = 1024 | |
canvas.height = 700 | |
context = canvas.getContext '2d' | |
points = [] | |
isMoving = false | |
$('canvas').on 'touchstart', (event) -> onTouchStart event | |
$('canvas').on 'touchmove', (event) -> onTouchMove event | |
onTouchStart = (event) => | |
points = [] | |
context.clearRect 0, 0, 1024, 700 | |
isMoving = true | |
points.push { x: event.changedTouches[0].clientX, y: event.changedTouches[0].clientY } | |
onTouchMove = (event) => | |
return unless isMoving | |
event.preventDefault(); | |
points.push { x: event.changedTouches[0].clientX, y: event.changedTouches[0].clientY } | |
drawLastLineSegment() | |
searchForIntersection() | |
drawLastLineSegment = -> | |
context.beginPath() | |
context.moveTo points[points.length - 2].x, points[points.length - 2].y | |
context.lineTo points[points.length - 1].x, points[points.length - 1].y | |
context.closePath() | |
context.stroke() | |
searchForIntersection = -> | |
pointA = points[points.length - 1] | |
pointB = points[points.length - 2] | |
for point, index in points | |
return if index is points.length - 3 | |
result = Intersection.intersectLineLine(pointA, pointB, point, points[index + 1]) | |
if result.points.length > 0 | |
isMoving = false | |
updateShape index, result.points[0] | |
drawShape() | |
rectangles() | |
updateShape = (index, point) -> | |
newPoints = points[index + 1..points.length] | |
points = newPoints | |
points[points.length - 1] = point | |
drawShape = -> | |
context.clearRect 0, 0, 1024, 700 | |
context.beginPath() | |
context.moveTo points[0].x, points[0].y | |
for point in points | |
context.lineTo point.x, point.y | |
context.closePath() | |
context.stroke() | |
rectangles = -> | |
minimumX = _.min(points, (point) -> point.x).x | |
maximumX = _.max(points, (point) -> point.x).x | |
minimumY = _.min(points, (point) -> point.y).y | |
maximumY = _.max(points, (point) -> point.y).y | |
width = maximumX - minimumX | |
height = maximumY - minimumY | |
context.strokeRect minimumX, minimumY, width, height | |
rectangles = [] | |
for i in [0..9] | |
for j in [0..9] | |
rectanglePointA = new Point2D minimumX + j * width / 10, minimumY + i * height / 10 | |
rectanglePointB = new Point2D minimumX + j * width / 10 + width / 10, minimumY + i * height / 10 + height / 10 | |
# console.log "hi", rectanglePointA, rectanglePointB | |
context.strokeRect rectanglePointA.x, rectanglePointA.y, rectanglePointB.x - rectanglePointA.x, rectanglePointB.y - rectanglePointA.y | |
result = Intersection.intersectPolygonRectangle(points, rectanglePointA, rectanglePointB) | |
rectangles.push { A: rectanglePointA, B: rectanglePointB } unless result.points.length is 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment