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
52 changes: 48 additions & 4 deletions modules/ecs6-class/line.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
const Point = require("./point");

class Line {
constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined }) {
constructor({ point1 = new Point(), point2 = new Point(), n = undefined, slope = undefined } = {}) {
if ((!(point1 instanceof Point && point2 instanceof Point)) | (!(point1 instanceof Point)) || (!(point2 instanceof Point))) {
throw new Error("type of point must to be point object")
}
if (n != undefined && typeof (n) != "number") {
throw new Error("type of n must to be number or undefined")
}
if (slope != undefined && typeof (slope) != "number") {
throw new Error("type of slope must to be number or undefined")
}
this.point1 = point1;
this.point2 = point2;
this.slope = slope;
this.n = n;
}

calculateSlope = () => {
calculateSlope() {
if (this.point1.x === this.point2.x) {
throw new Error("it is not possible to divide by zero")
}
this.slope = (this.point1.y - this.point2.y) / (this.point1.x - this.point2.x)
}

calculateNOfLineFunction = () => {
calculateNOfLineFunction() {
if (this.slope === undefined) {
throw new Error("must to send slope")
}
if (typeof (this.slope) != "number") {
throw new Error("the type must to be number")
}
this.n = this.point1.y - this.slope * this.point1.x
}

Expand All @@ -24,13 +42,39 @@ class Line {
return this.getPointByX(0)
}


getPointByX(x) {
if (typeof (x) != "number") {
throw new Error("type must be number")
}
if (this.slope === undefined && this.n === undefined) {
throw new Error("must to send slope and n")
}
if (this.n === undefined) {
throw new Error("must to send n")
}
if (this.slope === undefined) {
throw new Error("must to send slope")
}
let y = this.slope * x + this.n
return new Point({ x, y })
}

getPointByY(y) {
if (this.slope === undefined && this.n === undefined) {
throw new Error("must to send slope and n")
}
if (this.slope === undefined) {
throw new Error("must to send slope")
}
if (this.n === undefined) {
throw new Error("must to send n")
}
if (typeof (y) != "number") {
throw new Error("type must be number")
}
if (this.slope == 0) {
throw new Error("it is not possible to divide by zero")
}
let x = (y - this.n) / this.slope;
return new Point({ x, y })
}
Expand Down
15 changes: 15 additions & 0 deletions modules/ecs6-class/point.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
class Point {
constructor({x=0, y=0}={}) {
if((typeof(x)!="number" && typeof(y)!="number") || (typeof(x)!="number" && typeof(y)=="number") || (typeof(x)=="number" && typeof(y)!="number")){
throw new Error("the type must to be number")
}
this.x = x;
this.y = y;
}
moveVertical(value) {
if(value===undefined){
throw new Error("didn't send parameter")
}
if(typeof(value)!="number") {
throw new Error("type is not a number")
}
this.y += value;
}
moveHorizontal(value) {
if(value===undefined){
throw new Error("didn't send parameters")
}
if(typeof(value)!="number") {
throw new Error("type is not a number")
}
this.x += value;
}
}
Expand Down
32 changes: 30 additions & 2 deletions modules/geometry-calculation.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
const Line = require('./ecs6-class/line')
const Line = require('./ecs6-class/line');
const Point = require('./ecs6-class/point');

const calculateDistance = (point1, point2) => {
if (!(point1 instanceof Point) && !(point2 instanceof Point)) {
throw new Error("send in point1 and in point2 instance of point")
}
if (!(point1 instanceof Point)) {
throw new Error("send in point1 instance of point")
}
if (!(point2 instanceof Point)) {
throw new Error("send in point2 instance of point")
}
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;
}

const calculateJunctionPoint = (line1, line2) => {
if (!(line1 instanceof Line) && !(line2 instanceof Line)) {
throw new Error("send in line1 and in line2 instance of Line")
}
if (!(line1 instanceof Line)) {
throw new Error("send in line1 instance of Line")
}
if (!(line2 instanceof Line)) {
throw new Error("send in line2 instance of Line")
}
if (line1.slope === line2.slope) {
if (line1.n === line2.n) {
return true
Expand All @@ -24,6 +43,15 @@ const calculateJunctionPoint = (line1, line2) => {
}

const isPointOnLine = (line, point) => {
if (!(line instanceof Line) && !(point instanceof Point)) {
throw new Error("line must to be instance of Line and point instance of Point")
}
if (!(line instanceof Line)) {
throw new Error("line must to be instance of Line")
}
if (!(point instanceof Point)) {
throw new Error("point must to be instance of Point")
}
const proxyLine = new Line({ point1: line.point1, point2: point })
proxyLine.calculateSlope()
if (line.slope === proxyLine.slope) {
Expand Down
Loading