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
12 changes: 12 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const {createDefaultPreset} = require("ts-jest")

const tsJestTransformCfg = createDefaultPreset().transform;

/** @type {import ("jest").Config} **/
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
transform: {
...tsJestTransformCfg,
},
};
8 changes: 4 additions & 4 deletions modules/ecs6-class/line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ 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) {
Expand Down
15 changes: 9 additions & 6 deletions modules/geometry-calculation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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;
}
Expand All @@ -12,16 +12,19 @@ export const calculateJunctionPoint = (line1: Line, line2: Line): Boolean | Poin
if (line1.slope === line2.slope) {
if (line1.n === line2.n) {
return true
}
else {
} else {
return false
}
}
else {
if (line1.n !== undefined && line1.slope !== undefined && line2.n !== undefined && line2.slope !== undefined) {
} 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
} else {
throw new Error('Slope or n is undefined for one of the lines');
}
}
}
Expand Down
92 changes: 92 additions & 0 deletions modules/tests/geometry-calculation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import Point from '../ecs6-class/point';
import Line from '../ecs6-class/line';
import { calculateDistance, calculateJunctionPoint, isPointOnLine } from '../geometry-calculation';

describe('geometry-calculation module', () => {
test('calculateDistance returns correct distance', () => {
const p1 = new Point({ x: 0, y: 0 });
const p2 = new Point({ x: 3, y: 4 });
expect(calculateDistance(p1, p2)).toBe(5); // 3-4-5 triangle
});

test('calculateJunctionPoint returns true for identical lines', () => {
const p1 = new Point({ x: 0, y: 0 });
const p2 = new Point({ x: 2, y: 2 });
const line1 = new Line({ point1: p1, point2: p2 });
const line2 = new Line({ point1: p1, point2: p2 });
line1.slope = (p2.y - p1.y) / (p2.x - p1.x);
line1.n = p1.y - line1.slope * p1.x;
line2.slope = (p2.y - p1.y) / (p2.x - p1.x);
line2.n = p1.y - line2.slope * p1.x;
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 calculate here, call the functions inside the calculateJunctionPoint function

expect(calculateJunctionPoint(line1, line2)).toBe(true);
});

test('calculateJunctionPoint returns false for parallel lines', () => {
const p1 = new Point({ x: 0, y: 0 });
const p2 = new Point({ x: 2, y: 2 });
const p3 = new Point({ x: 0, y: 1 });
const p4 = new Point({ x: 2, y: 3 });
const line1 = new Line({ point1: p1, point2: p2 });
const line2 = new Line({ point1: p3, point2: p4 });
line1.slope = (p2.y - p1.y) / (p2.x - p1.x);
line1.n = p1.y - line1.slope * p1.x;
line2.slope = (p4.y - p3.y) / (p4.x - p3.x);
line2.n = p3.y - line2.slope * p3.x;
expect(calculateJunctionPoint(line1, line2)).toBe(false);
});

test('calculateJunctionPoint returns intersection point for crossing lines', () => {
const p1 = new Point({ x: 0, y: 0 });
const p2 = new Point({ x: 2, y: 2 });
const p3 = new Point({ x: 0, y: 2 });
const p4 = new Point({ x: 2, y: 0 });
const line1 = new Line({ point1: p1, point2: p2 });
const line2 = new Line({ point1: p3, point2: p4 });
line1.slope = (p2.y - p1.y) / (p2.x - p1.x);
line1.n = p1.y - line1.slope * p1.x;
line2.slope = (p4.y - p3.y) / (p4.x - p3.x);
line2.n = p3.y - line2.slope * p3.x;
const intersection = calculateJunctionPoint(line1, line2);
if (intersection instanceof Point) {
expect(intersection.x).toBeCloseTo(1);
expect(intersection.y).toBeCloseTo(1);
} else {
throw new Error('Intersection is not a Point');
}
});

test('isPointOnLine returns true for point on line', () => {
const p1 = new Point({ x: 0, y: 0 });
const p2 = new Point({ x: 2, y: 2 });
const line = new Line({ point1: p1, point2: p2 });
line.slope = (p2.y - p1.y) / (p2.x - p1.x);
line.n = p1.y - line.slope * p1.x;
const pointOnLine = new Point({ x: 1, y: 1 });
expect(isPointOnLine(line, pointOnLine)).toBe(true);
});

test('isPointOnLine returns false for point not on line', () => {
const p1 = new Point({ x: 0, y: 0 });
const p2 = new Point({ x: 2, y: 2 });
const line = new Line({ point1: p1, point2: p2 });
line.slope = (p2.y - p1.y) / (p2.x - p1.x);
line.n = p1.y - line.slope * p1.x;
const pointNotOnLine = new Point({ x: 1, y: 2 });
expect(isPointOnLine(line, pointNotOnLine)).toBe(false);
});

test('calculateJunctionPoint throws error for undefined slope or n', () => {
const p1 = new Point({ x: 0, y: 0 });
const p2 = new Point({ x: 2, y: 2 });
const line1 = new Line({ point1: p1, point2: p2 });
line1.slope = (p2.y - p1.y) / (p2.x - p1.x);
line1.n = p1.y - line1.slope * p1.x;
const line2 = new Line({ point1: p1, point2: p1 }); // קו עם אותו נקודה, שיפוע לא מוגדר
line2.slope = undefined; // או line2.n = undefined;

expect(() => calculateJunctionPoint(line1, line2)).toThrow(Error);
});



});
64 changes: 64 additions & 0 deletions modules/tests/line.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import Point from '../ecs6-class/point';
import Line from '../ecs6-class/line';

describe('Line Class', () => {
test('calculateSlope calculates correct slope', () => {
const p1 = new Point({ x: 0, y: 0 });
const p2 = new Point({ x: 2, y: 4 });
const line = new Line({ point1: p1, point2: p2 });
line.calculateSlope();
expect(line.slope).toBe((0 - 4) / (0 - 2)); // Should be 2
});

test('calculateNOfLineFunction calculates correct n', () => {
const p1 = new Point({ x: 1, y: 3 });
const p2 = new Point({ x: 2, y: 5 });
const line = new Line({ point1: p1, point2: p2 });
const slope = (p2.y - p1.y) / (p2.x - p1.x);
line.slope = slope;
line.calculateNOfLineFunction();
Copy link
Owner

Choose a reason for hiding this comment

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

2 functions

expect(line.n).toBe(p1.y - line.slope! * p1.x);
});

test('getPointByX returns correct point', () => {
const p1 = new Point({ x: 1, y: 3 });
const p2 = new Point({ x: 2, y: 5 });
const line = new Line({ point1: p1, point2: p2 });
line.slope = (p2.y - p1.y) / (p2.x - p1.x);
line.n = p1.y - line.slope * p1.x;
const point = line.getPointByX(10);
expect(point?.x).toBe(10);
expect(point?.y).toBeCloseTo(line.slope! * 10 + line.n!);
});

test('getPointByY returns correct point', () => {
const p1 = new Point({ x: 1, y: 3 });
const p2 = new Point({ x: 2, y: 5 });
const line = new Line({ point1: p1, point2: p2 });
line.slope = (p2.y - p1.y) / (p2.x - p1.x);
line.n = p1.y - line.slope * p1.x;
const point = line.getPointByY(7);
expect(point?.y).toBe(7);
expect(point?.x).toBeCloseTo((7 - line.n!) / line.slope!);
});

test('getPointOnXAsis returns point on X axis', () => {
const p1 = new Point({ x: 1, y: 3 });
const p2 = new Point({ x: 2, y: 5 });
const line = new Line({ point1: p1, point2: p2 });
line.slope = (p2.y - p1.y) / (p2.x - p1.x);
line.n = p1.y - line.slope * p1.x;
const point = line.getPointOnXAsis();
expect(point?.y).toBe(0);
});

test('getPointOnYAsis returns point on Y axis', () => {
const p1 = new Point({ x: 1, y: 3 });
const p2 = new Point({ x: 2, y: 5 });
const line = new Line({ point1: p1, point2: p2 });
line.slope = (p2.y - p1.y) / (p2.x - p1.x);
line.n = p1.y - line.slope * p1.x;
const point = line.getPointOnYAsis();
expect(point?.x).toBe(0);
});
});
31 changes: 31 additions & 0 deletions modules/tests/point.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Point from '../ecs6-class/point';

describe('Point Class', () => {
test('Default values of x and y should be 0', () => {
const point = new Point();
expect(point.x).toBe(0);
expect(point.y).toBe(0);
});

test('Should initialize x and y with provided values', () => {
const point = new Point({ x: 5, y: 10 });
expect(point.x).toBe(5);
expect(point.y).toBe(10);
});

test('moveVertical should correctly update y value', () => {
const point = new Point({ x: 0, y: 0 });
point.moveVertical(10);
expect(point.y).toBe(10);
point.moveVertical(-5);
expect(point.y).toBe(5);
});

test('moveHorizontal should correctly update x value', () => {
const point = new Point({ x: 0, y: 0 });
point.moveHorizontal(15);
expect(point.x).toBe(15);
point.moveHorizontal(-10);
expect(point.x).toBe(5);
});
});
Loading