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
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
17 changes: 10 additions & 7 deletions modules/ecs6-class/line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ export default class Line {


calculateSlope() {
this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x)
if (this.point1.x === this.point2.x) {
throw new Error("Vertical line - slope is undefined");
}
this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x);
}

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

Expand All @@ -33,14 +36,14 @@ export default class Line {


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.n !== undefined) {
let y = this.slope * x + this.n;
return new Point({ x, y });
}
}

getPointByY(y: number) {
if (this.slope && this.n) {
if (this.slope !== undefined && this.n !== undefined) {
let x = (y - this.n) / this.slope;
return new Point({ x, y })
}
Expand Down
53 changes: 35 additions & 18 deletions modules/geometry-calculation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,56 @@ 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 => {
// ודא שלשני הקווים יש slope ו-n
if (line1.slope === undefined) line1.calculateSlope?.();
if (line1.n === undefined) line1.calculateNOfLineFunction?.();
if (line2.slope === undefined) line2.calculateSlope?.();
if (line2.n === undefined) line2.calculateNOfLineFunction?.();

if (line1.slope === line2.slope) {
if (line1.n === line2.n) {
return true
}
else {
return false
return true;
} else {
return false;
}
}
else {
if (line1.n !== undefined && line1.slope !== undefined && line2.n !== undefined && line2.slope !== undefined) {
const x = (line1.n - line2.n) / (line2.slope - line1.slope)
} else {
if (
line1.n !== undefined && line1.slope !== undefined &&
line2.n !== undefined && line2.slope !== undefined
) {
const x = (line1.n - line2.n) / (line2.slope - line1.slope);
const junctionPoint = line1.getPointByX(x);
return junctionPoint
return junctionPoint;
}
}
}

export const isPointOnLine = (line: Line, point: Point): Boolean => {
const proxyLine = new Line({ point1: line.point1, point2: point })
proxyLine.calculateSlope()
if (line.slope === proxyLine.slope) {
proxyLine.calculateNOfLineFunction()
if (line.n === proxyLine.n) {
return true
if (line.slope === undefined) line.calculateSlope?.();
if (line.n === undefined) line.calculateNOfLineFunction?.();

const proxyLine = new Line({ point1: line.point1, point2: point });
proxyLine.calculateSlope();
if (
line.slope !== undefined &&
proxyLine.slope !== undefined &&
Math.abs(line.slope - proxyLine.slope) < 1e-10
) {
proxyLine.calculateNOfLineFunction();
if (
line.n !== undefined &&
proxyLine.n !== undefined &&
Math.abs(line.n - proxyLine.n) < 1e-10
) {
return true;
}
}
return false
return false;
}


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.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,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