Skip to content

Commit

Permalink
rename calc functions
Browse files Browse the repository at this point in the history
  • Loading branch information
201flaviosilva committed Feb 3, 2023
1 parent 6d9e577 commit f7e4b6e
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 93 deletions.
54 changes: 27 additions & 27 deletions src/Physics/calculateAngles.js
Expand Up @@ -10,13 +10,13 @@ import { radiansToDegrees } from "../Maths/radiansToDegrees.js";
*
* @returns {number} The angle between the two points, in radians.
*
* @example getAngleBetweenTwoPoints(0, 0, 1, 1); // output: 0.7853981633974483 (approx. 45°)
* @example getAngleBetweenTwoPoints(0, 0, 0, 1); // output: 1.5707963267948966 (approx. 90°)
* @example calcAngleBetweenTwoPoints(0, 0, 1, 1); // output: 0.7853981633974483 (approx. 45°)
* @example calcAngleBetweenTwoPoints(0, 0, 0, 1); // output: 1.5707963267948966 (approx. 90°)
*
* @function getAngleBetweenTwoPoints
* @memberof Physics
*/
export function getAngleBetweenTwoPoints(x1, y1, x2, y2) { return Math.atan2(y2 - y1, x2 - x1); }
export function calcAngleBetweenTwoPoints(x1, y1, x2, y2) { return Math.atan2(y2 - y1, x2 - x1); }

/**
* Calculates the angle between two points in a two-dimensional plane, given as instances of `Vector2`.
Expand All @@ -34,18 +34,18 @@ export function getAngleBetweenTwoPoints(x1, y1, x2, y2) { return Math.atan2(y2
* @example
* const point1 = { x: 0, y: 0 };
* const point2 = { x: 1, y: 1 };
* getAngleBetweenTwoPointsVector2(point1, point2); // output: 0.7853981633974483 (approx. 45°)
* calcAngleBetweenTwoPointsVector2(point1, point2); // output: 0.7853981633974483 (approx. 45°)
*
* @example
* const point1 = { x: 0, y: 0 };
* const point2 = { x: 0, y: 1 };
* getAngleBetweenTwoPointsVector2(point1, point2); // output: 1.5707963267948966 (approx. 90°)
* calcAngleBetweenTwoPointsVector2(point1, point2); // output: 1.5707963267948966 (approx. 90°)
*
* @function getAngleBetweenTwoPointsVector2
* @function calcAngleBetweenTwoPointsVector2
* @memberof Physics
*/
export function getAngleBetweenTwoPointsVector2(point1, point2) {
return getAngleBetweenTwoPoints(point1.x, point1.y, point2.x, point2.y);
export function calcAngleBetweenTwoPointsVector2(point1, point2) {
return calcAngleBetweenTwoPoints(point1.x, point1.y, point2.x, point2.y);
}

/**
Expand All @@ -58,14 +58,14 @@ export function getAngleBetweenTwoPointsVector2(point1, point2) {
*
* @returns {number} The angle between the two points, in degrees.
*
* @example getAngleBetweenTwoPointsDegrees(0, 0, 1, 1); // output: 45
* @example getAngleBetweenTwoPointsDegrees(0, 0, 0, 1); // output: 90
* @example calcAngleBetweenTwoPointsDegrees(0, 0, 1, 1); // output: 45
* @example calcAngleBetweenTwoPointsDegrees(0, 0, 0, 1); // output: 90
*
* @function getAngleBetweenTwoPointsDegrees
* @function calcAngleBetweenTwoPointsDegrees
* @memberof Physics
*/
export function getAngleBetweenTwoPointsDegrees(x1, y1, x2, y2) {
return radiansToDegrees(getAngleBetweenTwoPoints(x1, y1, x2, y2));
export function calcAngleBetweenTwoPointsDegrees(x1, y1, x2, y2) {
return radiansToDegrees(calcAngleBetweenTwoPoints(x1, y1, x2, y2));
}

/**
Expand All @@ -84,18 +84,18 @@ export function getAngleBetweenTwoPointsDegrees(x1, y1, x2, y2) {
* @example
* const point1 = { x: 0, y: 0 };
* const point2 = { x: 1, y: 1 };
* getAngleBetweenTwoPointsVector2Degrees(point1, point2); // output: 45
* calcAngleBetweenTwoPointsVector2Degrees(point1, point2); // output: 45
*
* @example
* const point1 = { x: 0, y: 0 };
* const point2 = { x: 0, y: 1 };
* getAngleBetweenTwoPointsVector2Degrees(point1, point2); // output: 90
* calcAngleBetweenTwoPointsVector2Degrees(point1, point2); // output: 90
*
* @function getAngleBetweenTwoPointsVector2Degrees
* @function calcAngleBetweenTwoPointsVector2Degrees
* @memberof Physics
*/
export function getAngleBetweenTwoPointsVector2Degrees(point1, point2) {
return getAngleBetweenTwoPointsDegrees(point1.x, point1.y, point2.x, point2.y);
export function calcAngleBetweenTwoPointsVector2Degrees(point1, point2) {
return calcAngleBetweenTwoPointsDegrees(point1.x, point1.y, point2.x, point2.y);
}


Expand All @@ -120,17 +120,17 @@ export function getAngleBetweenTwoPointsVector2Degrees(point1, point2) {
* @example
* const rect1 = { x: 0, y: 0, width: 2, height: 2 };
* const rect2 = { x: 2, y: 2, width: 2, height: 2 };
* calculateAngleBetweenRectangles(rect1, rect2); // output: 0.7853981633974483
* calcAngleBetweenRectangles(rect1, rect2); // output: 0.7853981633974483
*
* @example
* const rect1 = { x: 0, y: 0, width: 2, height: 2 };
* const rect2 = { x: 1, y: 2, width: 2, height: 2 };
* calculateAngleBetweenRectangles(rect1, rect2); // output: 1.1071487177940904
* calcAngleBetweenRectangles(rect1, rect2); // output: 1.1071487177940904
*
* @function calculateAngleBetweenRectangles
* @function calcAngleBetweenRectangles
* @memberof Physics
*/
export function calculateAngleBetweenRectangles(rect1, rect2) {
export function calcAngleBetweenRectangles(rect1, rect2) {
const xOverlap = Math.min(rect1.x + rect1.width, rect2.x + rect2.width) - Math.max(rect1.x, rect2.x);
const yOverlap = Math.min(rect1.y + rect1.height, rect2.y + rect2.height) - Math.max(rect1.y, rect2.y);
return Math.atan2(yOverlap, xOverlap);
Expand All @@ -150,14 +150,14 @@ export function calculateAngleBetweenRectangles(rect1, rect2) {
*
* @returns {number} The angle between the two rectangles.
*
* @example calculateAngleBetweenRectanglesByCoordinates(0, 0, 10, 10, 20, 20, 20, 20); // Output: 45
* @example calculateAngleBetweenRectanglesByCoordinates(10, 10, 10, 10, 30, 30, 20, 20); // Output: 30
* @example calcAngleBetweenRectanglesByCoordinates(0, 0, 10, 10, 20, 20, 20, 20); // Output: 45
* @example calcAngleBetweenRectanglesByCoordinates(10, 10, 10, 10, 30, 30, 20, 20); // Output: 30
*
* @function calculateAngleBetweenRectanglesByCoordinates
* @function calcAngleBetweenRectanglesByCoordinates
* @memberof Physics
*/
export function calculateAngleBetweenRectanglesByCoordinates(x1, y1, width1, height1, x2, y2, width2, height2) {
return calculateAngleBetweenRectangles(
export function calcAngleBetweenRectanglesByCoordinates(x1, y1, width1, height1, x2, y2, width2, height2) {
return calcAngleBetweenRectangles(
{ x: x1 + width1 / 2, y: y1 + height1 / 2, width: width1, height: height1 },
{ x: x2 + width2 / 2, y: y2 + height2 / 2, width: width2, height: height2 }
);
Expand Down
102 changes: 52 additions & 50 deletions src/Physics/calculateDimensions.js
Expand Up @@ -5,13 +5,13 @@
*
* @returns {number} The area of the circle.
*
* @example calculateCircleArea(5); // Output: 78.53981633974483
* @example calculateCircleArea(10); // Output: 314.1592653589793
* @example calcCircleArea(5); // Output: 78.53981633974483
* @example calcCircleArea(10); // Output: 314.1592653589793
*
* @function calculateCircleArea
* @function calcCircleArea
* @memberof Physics
*/
export function calculateCircleArea(radius) { return Math.PI * radius * radius; }
export function calcCircleArea(radius) { return Math.PI * radius * radius; }

/**
* Gets the area of a rectangle.
Expand All @@ -21,13 +21,13 @@ export function calculateCircleArea(radius) { return Math.PI * radius * radius;
*
* @returns {number} The area of the rectangle.
*
* @example calculateRectangleArea(5, 10); // Output: 50
* @example calculateRectangleArea(10, 20);
* @example calcRectangleArea(5, 10); // Output: 50
* @example calcRectangleArea(10, 20);
*
* @function calculateRectangleArea
* @function calcRectangleArea
* @memberof Physics
*/
export function calculateRectangleArea(width, height) { return width * height; }
export function calcRectangleArea(width, height) { return width * height; }


/**
Expand All @@ -37,13 +37,13 @@ export function calculateRectangleArea(width, height) { return width * height; }
*
* @returns {number} The perimeter of the circle.
*
* @example calculateCirclePerimeter(5); // Output: 31.41592653589793
* @example calculateCirclePerimeter(10); // Output: 62.83185307179586
* @example calcCirclePerimeter(5); // Output: 31.41592653589793
* @example calcCirclePerimeter(10); // Output: 62.83185307179586
*
* @function calculateCirclePerimeter
* @function calcCirclePerimeter
* @memberof Physics
*/
export function calculateCirclePerimeter(radius) { return 2 * Math.PI * radius; }
export function calcCirclePerimeter(radius) { return 2 * Math.PI * radius; }

/**
* Gets the perimeter of a rectangle.
Expand All @@ -53,13 +53,13 @@ export function calculateCirclePerimeter(radius) { return 2 * Math.PI * radius;
*
* @returns {number} The perimeter of the rectangle.
*
* @example calculateRectanglePerimeterByDimensions(5, 10); // Output: 30
* @example calculateRectanglePerimeterByDimensions(10, 20); // Output: 60
* @example calcRectanglePerimeterByDimensions(5, 10); // Output: 30
* @example calcRectanglePerimeterByDimensions(10, 20); // Output: 60
*
* @function calculateRectanglePerimeterByDimensions
* @function calcRectanglePerimeterByDimensions
* @memberof Physics
*/
export function calculateRectanglePerimeterByDimensions(width, height) { return 2 * (width + height); }
export function calcRectanglePerimeterByDimensions(width, height) { return 2 * (width + height); }

/**
* Gets the perimeter of a rectangle defined by its bounds.
Expand All @@ -74,17 +74,17 @@ export function calculateRectanglePerimeterByDimensions(width, height) { return
*
* @example
* const rectangle = { x: 0, y: 0, width: 5, height: 10 };
* calculateRectanglePerimeter(rectangle); // Output: 30
* calcRectanglePerimeter(rectangle); // Output: 30
*
* @example
* const rectangle = { x: 10, y: 20, width: 10, height: 20 };
* calculateRectanglePerimeter(rectangle); // Output: 60
* calcRectanglePerimeter(rectangle); // Output: 60
*
* @function calculateRectanglePerimeter
* @function calcRectanglePerimeter
* @memberof Physics
*/
export function calculateRectanglePerimeter(rectangle) {
return calculateRectanglePerimeterByDimensions(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
export function calcRectanglePerimeter(rectangle) {
return calcRectanglePerimeterByDimensions(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
}


Expand All @@ -96,12 +96,12 @@ export function calculateRectanglePerimeter(rectangle) {
*
* @returns {number} The center x-coordinate of the rectangle
*
* @example getRectangleCenterX(10, 20); // returns 15
* @example calcRectangleCenterX(10, 20); // returns 15
*
* @function getRectangleCenterX
* @function calcRectangleCenterX
* @memberof Physics
*/
export function getRectangleCenterX(x, width) { return x + width / 2; }
export function calcRectangleCenterX(x, width) { return x + width / 2; }

/**
* Calculates the center y-coordinate of a rectangle given its y-coordinate and height.
Expand All @@ -111,12 +111,12 @@ export function getRectangleCenterX(x, width) { return x + width / 2; }
*
* @returns {number} The center y-coordinate of the rectangle
*
* @example getRectangleCenterY(10, 20); // returns 15
* @example calcRectangleCenterY(10, 20); // returns 15
*
* @function getRectangleCenterY
* @function calcRectangleCenterY
* @memberof Physics
*/
export function getRectangleCenterY(y, height) { return y + height / 2; }
export function calcRectangleCenterY(y, height) { return y + height / 2; }

/**
* Calculates the center coordinates of a rectangle given its x-coordinate, y-coordinate, width, and height.
Expand All @@ -128,12 +128,12 @@ export function getRectangleCenterY(y, height) { return y + height / 2; }
*
* @returns {object} An object with x and y properties representing the center coordinates of the rectangle
*
* @example getRectangleCenter(10, 10, 20, 20); // returns { x: 15, y: 15 }
* @example calcRectangleCenter(10, 10, 20, 20); // returns { x: 15, y: 15 }
*
* @function getRectangleCenter
* @function calcRectangleCenter
* @memberof Physics
*/
export function getRectangleCenter(x, y, width, height) { return { x: getRectangleCenterX(x, width), y: getRectangleCenterY(y, height) }; }
export function calcRectangleCenter(x, y, width, height) { return { x: calcRectangleCenterX(x, width), y: calcRectangleCenterY(y, height) }; }

/**
* Calculates the center x-coordinate of a rectangle given its bounds.
Expand All @@ -142,12 +142,12 @@ export function getRectangleCenter(x, y, width, height) { return { x: getRectang
*
* @returns {number} The center x-coordinate of the rectangle
*
* @example getRectangleCenterXFromBounds({ x: 10, y: 10, width: 20, height: 20 }); // returns 15
* @example calcRectangleCenterXFromBounds({ x: 10, y: 10, width: 20, height: 20 }); // returns 15
*
* @function getRectangleCenterXFromBounds
* @function calcRectangleCenterXFromBounds
* @memberof Physics
*/
export function getRectangleCenterXFromBounds(rectangle) { return getRectangleCenterX(rectangle.x, rectangle.width); }
export function calcRectangleCenterXFromBounds(rectangle) { return calcRectangleCenterX(rectangle.x, rectangle.width); }

/**
* Calculates the center y-coordinate of a rectangle given its bounds.
Expand All @@ -156,12 +156,12 @@ export function getRectangleCenterXFromBounds(rectangle) { return getRectangleCe
*
* @returns {number} The center y-coordinate of the rectangle
*
* @example getRectangleCenterYFromBounds({ x: 10, y: 10, width: 20, height: 20 }); // returns 15
* @example calcRectangleCenterYFromBounds({ x: 10, y: 10, width: 20, height: 20 }); // returns 15
*
* @function getRectangleCenterYFromBounds
* @function calcRectangleCenterYFromBounds
* @memberof Physics
*/
export function getRectangleCenterYFromBounds(rectangle) { return getRectangleCenterY(rectangle.y, rectangle.height); }
export function calcRectangleCenterYFromBounds(rectangle) { return calcRectangleCenterY(rectangle.y, rectangle.height); }

/**
* Returns the Y-coordinate of the center of a rectangle, given the rectangle's bounds.
Expand All @@ -170,13 +170,13 @@ export function getRectangleCenterYFromBounds(rectangle) { return getRectangleCe
* @returns {number} The Y-coordinate of the center of the rectangle.
*
* @example
* getRectangleCenterYFromBounds({ x: 10, y: 10, y: 10, height: 20 }); // 20
* calcRectangleCenterYFromBounds({ x: 10, y: 10, y: 10, height: 20 }); // 20
*
* @function getRectangleCenterYFromBounds
* @function calcRectangleCenterYFromBounds
* @memberof Physics
*/
export function getRectangleCenterYFromBounds(rectangle) {
return getRectangleCenterY(rectangle.y, rectangle.height);
export function calcRectangleCenterYFromBounds(rectangle) {
return calcRectangleCenterY(rectangle.y, rectangle.height);
}

/**
Expand All @@ -187,13 +187,13 @@ export function getRectangleCenterYFromBounds(rectangle) {
*
* @example
* const rect = { x: 10, y: 10, width: 20, height: 20 };
* getRectangleCenterFromBounds(rect); // { x: 20, y: 20 }
* calcRectangleCenterFromBounds(rect); // { x: 20, y: 20 }
*
* @function getRectangleCenterFromBounds
* @function calcRectangleCenterFromBounds
* @memberof Physics
*/
export function getRectangleCenterFromBounds(rectangle) {
return getRectangleCenter(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
export function calcRectangleCenterFromBounds(rectangle) {
return calcRectangleCenter(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
}

/**
Expand All @@ -206,12 +206,12 @@ export function getRectangleCenterFromBounds(rectangle) {
*
* @returns {Array} An array of objects that represent the vertices of the rectangle, where each object has properties 'x' and 'y'.
*
* @example getRectangleVertices(0, 0, 2, 3); // [{x: 0, y: 0}, {x: 2, y: 0}, {x: 2, y: 3}, {x: 0, y: 3}]
* @example calcRectangleVertices(0, 0, 2, 3); // [{x: 0, y: 0}, {x: 2, y: 0}, {x: 2, y: 3}, {x: 0, y: 3}]
*
* @function getRectangleVertices
* @function calcRectangleVertices
* @memberof Physics
*/
export function getRectangleVertices(x, y, width, height) {
export function calcRectangleVertices(x, y, width, height) {
return [
{ x: x, y: y },
{ x: x + width, y: y },
Expand All @@ -227,9 +227,11 @@ export function getRectangleVertices(x, y, width, height) {
*
* @returns {Array} An array of objects that represent the vertices of the rectangle, where each object has properties 'x' and 'y'.
*
* @example getVerticesFromRectangleBounds({ x: 0, y: 0, width: 2, height: 3 }); // [{x: 0, y: 0}, {x: 2, y: 0}, {x: 2, y: 3}, {x: 0, y: 3}]
* @example calcVerticesFromRectangleBounds({ x: 0, y: 0, width: 2, height: 3 }); // [{x: 0, y: 0}, {x: 2, y: 0}, {x: 2, y: 3}, {x: 0, y: 3}]
*
* @function getVerticesFromRectangleBounds
* @function calcVerticesFromRectangleBounds
* @memberof Physics
*/
export function getVerticesFromRectangleBounds(rectangle) { return getRectangleVertices(rectangle.x, rectangle.y, rectangle.width, rectangle.height); }
export function calcVerticesFromRectangleBounds(rectangle) {
return calcRectangleVertices(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
}

0 comments on commit f7e4b6e

Please sign in to comment.