Last active
May 1, 2019 21:00
-
-
Save ThomasOrlita/9d295b444cf7add335f6cac3355a56ba to your computer and use it in GitHub Desktop.
geometry collisions and functions for math/p5js
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
function collisionPointCircle(pointX, pointY, circleX, circleY, circleR) { | |
return p5.prototype.dist(pointX, pointY, circleX, circleY) <= circleR; | |
} | |
function collisionCirclePoint(circleX, circleY, circleR, pointX, pointY) { | |
return collisionPointCircle(pointX, pointY, circleX, circleY, circleR); | |
} | |
function collisionPointLine(pointX, pointY, lineX1, lineY1, lineX2, lineY2) { | |
var d1 = dist(pointX, pointY, lineX1, lineY1); | |
var d2 = dist(pointX, pointY, lineX2, lineY2); | |
var d = dist(lineX1, lineY1, lineX2, lineY2); | |
return (d1 + d2) - d <= 0.01; | |
} | |
function collisionLinePoint(lineX1, lineY1, lineX2, lineY2, pointX, pointY) { | |
return collisionPointLine(pointX, pointY, lineX1, lineY1, lineX2, lineY2); | |
} | |
function collisionCircleCircle(circle1X, circle1Y, circle1R, circle2X, circle2Y, circle2R) { | |
return p5.prototype.dist(circle1X, circle1Y, circle2X, circle2Y) <= circle1R + circle2R; | |
} | |
function collisionCircleRect(circleX, circleY, circleR, rectX, rectY, rectWidth, rectHeight) { | |
var px = max(rectX, min(circleX, rectX + rectWidth)); | |
var py = max(rectY, min(circleY, rectY + rectHeight)); | |
return p5.prototype.dist(px, py, circleX, circleY) < circleR; | |
} | |
function collisionRectCircle(rectX, rectY, rectWidth, rectHeight, circleX, circleY, circleR) { | |
return collisionCircleRect(circleX, circleY, circleR, rectX, rectY, rectWidth, rectHeight); | |
} | |
function collisionPointRect(pointX, pointY, rectX, rectY, rectWidth, rectHeight) { | |
return pointX > rectX && pointX < rectX + rectWidth && | |
pointY > rectY && pointY < rectY + rectHeight; | |
} | |
function collisionRectPoint(rectX, rectY, rectWidth, rectHeight, pointX, pointY) { | |
return collisionPointRect(pointX, pointY, rectX, rectY, rectWidth, rectHeight); | |
} | |
function collisionRectRect(rect1X, rect1Y, rect1Width, rect1Height, rect2X, rect2Y, rect2Width, rect2Height) { | |
return (rect1X < rect2X + rect2Width && | |
rect1X + rect1Width > rect2X && | |
rect1Y < rect2Y + rect2Height && | |
rect1Y + rect1Height > rect2Y); | |
} | |
/** | |
* Conserve aspect ratio of the original region. Useful when shrinking/enlarging | |
* images to fit into a certain area. | |
* | |
* @param {Number} srcWidth width of source image | |
* @param {Number} srcHeight height of source image | |
* @param {Number} maxWidth maximum available width | |
* @param {Number} maxHeight maximum available height | |
* @return {Object} { width, height } | |
*/ | |
function calculateAspectRatioFit(srcWidth, srcHeight, maxWidth, maxHeight) { | |
var ratio = Math.min(maxWidth / srcWidth, maxHeight / srcHeight); | |
return { width: srcWidth*ratio, height: srcHeight*ratio }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment