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
8 changes: 8 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\\.ts$': 'ts-jest', // אם אתה משתמש ב-TypeScript
},
moduleFileExtensions: ['js', 'ts'],
};
50 changes: 17 additions & 33 deletions modules/ecs6-class/line.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,34 @@
import Point from "./point";

export default class Line {
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 } = {}) {
class Line {
point1: Point;
point2: Point;
n: number;
slope: number;
constructor({ point1, point2 }: { point1: Point, point2: Point }) {
this.point1 = point1;
this.point2 = point2;
this.slope = slope;
this.n = n;
this.calculateSlope();
this.calculateNOfLineFunction();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be more specific, the line can be updated by the points (each point can be moved) so you'll have to calculate this functions again

}



calculateSlope() {
this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x)
this.slope = (this.point2.y - this.point1.y) / (this.point2.x - this.point1.x);
}

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

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

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


getPointByX(x: number) {
if (this.slope && this.n) {
let y = this.slope * x + this.n
return new Point({ x, y })
}
let 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 })
}
let x = (y - this.n) / this.slope;
return new Point({ x, y });
}


}
export default Line;
10 changes: 5 additions & 5 deletions modules/ecs6-class/point.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
export default class Point {
class Point {
x: number
y: number
constructor({ x = 0, y = 0 }:{x?:number, y?:number} = {}) {
constructor({ x = 0, y = 0 }: { x?: number, y?: number } = {}) {
this.x = x;
this.y = y;
}
moveVertical(value:number) {
moveVertical(value: number) {
this.y += value;
}
moveHorizontal(value:number) {
moveHorizontal(value: number) {
this.x += value;
}
}

export default Point;
48 changes: 17 additions & 31 deletions modules/geometry-calculation.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,27 @@
import Line from './ecs6-class/line';
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 distanceX = Math.abs(point2.x - point1.x) ** 2;
let distanceY = Math.abs(point2.y - point1.y) ** 2;
const distance = Math.sqrt(distanceX + distanceY);
return distance;
}

export const calculateJunctionPoint = (line1: Line, line2: Line): Boolean | Point | undefined => {
if (line1.slope === line2.slope) {
if (line1.n === line2.n) {
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)
const junctionPoint = line1.getPointByX(x);
return junctionPoint
}
export const isPointOnLine = (line: Line, point: Point): Boolean => {
if (typeof line.slope !== 'number' || typeof line.n !== 'number' || typeof point.x !== 'number' || typeof point.y !== 'number') {
throw new Error('Invalid input types');
}
const expectedY = line.slope * point.x + line.n;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't remove the functions
they were build to be used in different modules
you have to mock them in the tests

const tolerance = 0.0001;
return Math.abs(point.y - expectedY) < tolerance;
}

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
export const calculateJunctionPoint = (line1: Line, line2: Line): Point | false | true => {
if (line1.slope === line2.slope) {
if (line1.n === line2.n) {
return true;
}
return false;
}
return false
}


const x = (line2.n - line1.n) / (line1.slope - line2.slope);
const y = line1.slope * x + line1.n;
return new Point({ x, y });
}
Loading