Skip to content

Commit

Permalink
Move files new functions and classes
Browse files Browse the repository at this point in the history
  • Loading branch information
yetnt committed Oct 17, 2024
1 parent a875ffc commit 92c8747
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 93 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yetnt/ump",
"version": "6.1.0-dev.0",
"version": "6.1.0-dev.1",
"description": "A very useless math package for your complex javascript projects",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
69 changes: 53 additions & 16 deletions src/functions/Geometry/Line.ts → src/Geometry/Line.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Point, fResult, BaseLine } from ".";
import { Point, fResult, BaseLine } from "./base";

/**
* A line, what else.
Expand Down Expand Up @@ -40,10 +40,10 @@ export default class Line extends BaseLine {
} else if (typeof param1 == "number" && typeof param2 == "number") {
let m = param1;
let c = param2;
this.calc(m, c);

this._start = new Point((0 - c) / m, 0);
this._end = new Point(0, c);
this.calc(m, c);
}
}

Expand Down Expand Up @@ -71,30 +71,65 @@ export default class Line extends BaseLine {
return this._end;
}

/**
* Checks if the given points lie on this line.
*
* @param pts - The points to check.
* @returns `true` if all the points lie on this line, `false` otherwise.
*
* @remarks
* This method uses the equation of the line (y = mx + c) to check if the points lie on the line.
* It iterates through each point and checks if the y-coordinate of the point equals the calculated y-coordinate using the point's x-coordinate and the gradient and y-intercept of the line.
* If all points satisfy this condition, the method returns `true`; otherwise, it returns `false`.
*/
includes(...pts: Point[]): boolean {
return pts.some((pt) => {
return this.gradient * pt.x + this.yIntercept == pt.y;
});
}

/**
* Calculates the gradient and midpoint of the line based on the provided start and end points, or gradient and y-intercept.
*
* @param m - Optional parameter representing the gradient of the line. If provided, it will be used to calculate the gradient.
* @param c - Optional parameter representing the y-intercept of the line. If provided, it will be used to calculate the gradient.
*
* @returns
*
* @remarks
* If both `m` and `c` are provided, the method will use `m` to calculate the gradient.
* If neither `m` nor `c` are provided, the method will calculate the gradient using the start and end points of the line.
* The midpoint of the line is also calculated and stored in the `midpoint` property.
*/
private calc(m?: number, c?: number) {
this.gradient =
m || this._end.y == this._start.y // gradient will be undefined.
m != undefined
? m
: this._end.y == this._start.y // gradient will be undefined.
? undefined
: (this._end.x - this._start.x) / (this._end.y - this._start.y);
this.midpoint = new Point(
(this._start.x + this._end.x) / 2,
(this._start.y + this._end.y) / 2
);

this.yIntercept = c || this.start.y - this.gradient * this.start.x;
this.yIntercept =
c != undefined ? c : this.start.y - this.gradient * this.start.x;
this.xIntercept =
this.yIntercept === 0
? this.start.x
: -this.yIntercept / this.gradient;
}

/**
* Function that finds an unknown coordinate from the given point and a given midpoint.
* @param givenPt Given point
* @param midpt Given midpoint
* @param givenX Given X (If you're looking for y)
* @param givenY Given Y (if you're looking for x, if you do enter a value make givenX = undefined)
* @returns
* A static method that finds a new point based on a given point, midpoint, and either the x or y coordinate.
* It calculates the missing coordinate using the midpoint and the given point.
*
* @param givenPt - The known point on the line.
* @param midpt - The midpoint of the line.
* @param givenX - The x-coordinate of the new point. If not provided, the method will calculate it.
* @param givenY - The y-coordinate of the new point. If not provided, the method will calculate it.
* @returns An object containing the new point and the line formed by the given point and the new point.
*/
static findPtFromMidpt(
givenPt: Point,
Expand Down Expand Up @@ -125,12 +160,14 @@ export default class Line extends BaseLine {
}

/**
* Function that finds an unknown coordinate from the given point and a given gradient.
* @param givenPt Given Point
* @param gradient Given Gradient
* @param givenX Given X (If you're looking for y)
* @param givenY Given Y (if you're looking for x, if you do enter a value make givenX = undefined)
* @returns
* A static method that finds a new point based on a given point, gradient, and either the x or y coordinate.
* It calculates the missing coordinate using the given gradient and the given point.
*
* @param givenPt - The known point on the line.
* @param gradient - The gradient of the line.
* @param givenX - The x-coordinate of the new point. If not provided, the method will calculate it.
* @param givenY - The y-coordinate of the new point. If not provided, the method will calculate it.
* @returns An object containing the new point and the line formed by the given point and the new point.
*/
static findPtFromGradient(
givenPt: Point,
Expand Down
17 changes: 3 additions & 14 deletions src/functions/Geometry/index.ts → src/Geometry/base.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { isParallel, isPerpendicular, intersectionPt } from "./funcs";
import Line from "./Line";

/**
* Represents a single point in this package.
*/
class Point {
export class Point {
/**
* X Coordinate
*/
Expand All @@ -28,7 +27,7 @@ class Point {
/**
* Interface that is returned by findPtFromMidpt and findPtFromGradient
*/
interface fResult {
export interface fResult {
/**
* The entire line this makes
*/
Expand All @@ -42,7 +41,7 @@ interface fResult {
/**
* Where all things are extended from.
*/
class BaseLine {
export class BaseLine {
/**
* Y intercept.
*/
Expand All @@ -52,13 +51,3 @@ class BaseLine {
*/
xIntercept: number;
}

export {
Point,
fResult,
Line,
isParallel,
isPerpendicular,
intersectionPt,
BaseLine,
};
94 changes: 94 additions & 0 deletions src/Geometry/funcs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { Point } from "./base";
import Line from "./Line";

/**
* Checks if two lines are perpendicular.
*
* This function determines if two lines are perpendicular by comparing their gradients.
* If one line has an undefined gradient and the other has a gradient of 0, they are considered perpendicular.
* For non-horizontal/vertical lines, the function checks if the product of their gradients is -1.
*
* @param l1 - The first line.
* @param l2 - The second line.
* @returns `true` if the lines are perpendicular, `false` otherwise.
*/
export function isPerpendicular(l1: Line, l2: Line): boolean {
// If one line has an undefined gradient and the other has a gradient of 0
if (
(l1.gradient === undefined && l2.gradient === 0) ||
(l1.gradient === 0 && l2.gradient === undefined)
) {
return true; // These lines are perpendicular (horizontal & vertical)
}
// Standard perpendicularity check for non-horizontal/vertical lines
return l1.gradient * l2.gradient === -1;
}

/**
* Checks if two lines are parallel.
*
* This function compares the gradients of the two input lines.
* If the gradients are equal, the lines are considered parallel.
*
* @param l1 - The first line.
* @param l2 - The second line.
* @returns `true` if the lines are parallel, `false` otherwise.
*/
export function isParallel(l1: Line, l2: Line): boolean {
return l1.gradient == l2.gradient;
}

/**
* Calculates the point of intersection between two lines.
*
* This function uses the gradients and y-intercepts of the two lines to find the point where they intersect.
* If the lines are parallel, the function returns `undefined`.
*
* @param l1 - The first line.
* @param l2 - The second line.
* @returns The point of intersection between `l1` and `l2`, or `undefined` if the lines are parallel.
*/
export function intersectionPt(l1: Line, l2: Line): Point | undefined {
const epsilon = 1e-10; // tolerance for floating-point errors
if (isParallel(l1, l2)) {
return undefined; // Lines are parallel, no intersection
}

// if we have line y=mx+c and y=mx+c
// when their equal essential m1x+c1 = m2x + c2
// c can be found by looking at what y value is hit when x is 0
// m1x + c1 - c2 = m2x
// c1 - c2 = m2x - m1x
// c1 - c2 = x (m2 - m1)
// (c1-c2)/(m2-m1) = x.
// Then when x is found, input it into the findPtFromGradient to find y.

// Calculate x using the formula for the intersection of two lines
const gradientDiff = l2.gradient - l1.gradient;

if (Math.abs(gradientDiff) < epsilon) {
return undefined; // Gradients are too close, treat as parallel
}

const x = (l1.yIntercept - l2.yIntercept) / gradientDiff;

// Use either line to calculate the y value (I'll use line 1)
const y = l1.gradient * x + l1.yIntercept;

return new Point(x, y);
}

/**
* Checks if three points are collinear.
*
* This function creates two lines using the first and second points, and the second and third points.
* It then compares the gradients of these two lines. If the gradients are equal, the points are collinear.
*
* @param a - The first point.
* @param b - The second point.
* @param c - The third point.
* @returns `true` if the points are collinear, `false` otherwise.
*/
export function arePointsCollinear(a: Point, b: Point, c: Point): boolean {
return new Line(a, b).gradient === new Line(b, c).gradient;
}
13 changes: 13 additions & 0 deletions src/Geometry/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Point, fResult, BaseLine } from "./base";
import { isParallel, isPerpendicular, intersectionPt } from "./funcs";
import Line from "./Line";

export {
Point,
fResult,
Line,
isParallel,
isPerpendicular,
intersectionPt,
BaseLine,
};
60 changes: 0 additions & 60 deletions src/functions/Geometry/funcs.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/functions/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,3 @@ export * from "./mathEval";

import stats from "./Stats/exports";
export const Stats = stats;

export * from "./Geometry";
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

export * from "./functions/exports";
export * from "./libmath/enums";

export * from "./Geometry";

0 comments on commit 92c8747

Please sign in to comment.