Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { createDefaultPreset } = require("ts-jest");

const tsJestTransformCfg = createDefaultPreset().transform;

/** @type {import("jest").Config} **/
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
61 changes: 37 additions & 24 deletions modules/ecs6-class/line.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,63 @@
import Point from "./point";

export default class Line {
point1: Point
point2: Point
n: number | undefined
slope: number | undefined
point1: Point;
point2: Point;
n: number | undefined;
slope: number | undefined;

constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }: { point1?: Point, point2?: Point, n?: number, slope?: number } = {}) {
this.point1 = point1;
this.point2 = point2;
this.slope = slope;
this.n = n;
}



setPoints(p1: Point, p2: Point) {
this.point1 = p1;
this.point2 = p2;
}
calculateSlope() {
this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x)
const dx = this.point2.x - this.point1.x;
if (dx === 0) {
this.slope = undefined;
} else {
this.slope = (this.point2.y - this.point1.y) / dx;
}
return this.slope;
}

calculateNOfLineFunction() {
if (this.slope)
this.n = this.point1.y - this.slope * this.point1.x
this.calculateSlope();
if (this.slope !== undefined) {
this.n = this.point1.y - this.slope * this.point1.x;
} else {
this.n = undefined;
}
}

getPointOnXAsis() {
return this.getPointByY(0)
this.calculateNOfLineFunction();
return this.getPointByY(0);
}

getPointOnYAsis() {
return this.getPointByX(0)
this.calculateNOfLineFunction();
return this.getPointByX(0);
}


getPointByX(x: number) {
if (this.slope && this.n) {
let y = this.slope * x + this.n
return new Point({ x, y })
}
if (this.slope === undefined) this.calculateSlope();
if (this.n === undefined) this.calculateNOfLineFunction();
if (this.slope === undefined || this.n === undefined) return undefined;
const y = this.slope * x + this.n;
return new Point({ x, y });
}

getPointByY(y: number) {
if (this.slope && this.n) {
let x = (y - this.n) / this.slope;
return new Point({ x, y })
}
if (this.slope === undefined) this.calculateSlope();
if (this.n === undefined) this.calculateNOfLineFunction();
if (this.slope === undefined || this.n === undefined) return undefined;
const x = (y - this.n) / this.slope;
return new Point({ x, y });
}


}
}
6 changes: 5 additions & 1 deletion modules/geometry-calculation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import Point from './ecs6-class/point';

export const calculateDistance = (point1: Point, point2: Point): number => {
let distanceX = (point2.x - point1.x) ** 2;
let distanceY = (point2.y - point2.y) ** 2;
let distanceY = (point2.y - point1.y) ** 2;
const distance = Math.sqrt(distanceX + distanceY);
return distance;
}

export const calculateJunctionPoint = (line1: Line, line2: Line): Boolean | Point | undefined => {
line1.calculateNOfLineFunction();
line2.calculateNOfLineFunction();
if (line1.slope === line2.slope) {
if (line1.n === line2.n) {
return true
Expand All @@ -24,9 +26,11 @@ export const calculateJunctionPoint = (line1: Line, line2: Line): Boolean | Poin
return junctionPoint
}
}
return undefined;
}

export const isPointOnLine = (line: Line, point: Point): Boolean => {
line.calculateNOfLineFunction();
const proxyLine = new Line({ point1: line.point1, point2: point })
proxyLine.calculateSlope()
if (line.slope === proxyLine.slope) {
Expand Down
189 changes: 189 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "jest"
"test": "jest",
"coverage": "jest --coverage"
},
"repository": {
"type": "git",
Expand All @@ -22,6 +23,7 @@
"devDependencies": {
"@types/jest": "^30.0.0",
"jest": "^30.0.4",
"ts-jest": "^29.4.0",
"ts-node": "^10.9.2",
"typescript": "^5.8.3"
}
Expand Down
Loading