mirror of
https://github.com/Mastermindzh/examples.git
synced 2025-09-02 21:34:30 +02:00
consolidation of various example repos/gists
This commit is contained in:
52
typescript/geo/radiusToPolygon/helpers.ts
Normal file
52
typescript/geo/radiusToPolygon/helpers.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { Point } from "./point";
|
||||
|
||||
/**
|
||||
* Converts an angle in radians to an angle in degrees by multiplying with 180 and dividing by 180
|
||||
* @param angleInRadians
|
||||
*/
|
||||
export const radiansToDegrees = (angleInRadians: number) => {
|
||||
return (angleInRadians * 180) / Math.PI;
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert an angle in degrees to an angle in radians by multiplying by PI and dividing the result by 180
|
||||
* https://www.mathwarehouse.com/trigonometry/radians/convert-degee-to-radians.php
|
||||
* @param angleInDegrees
|
||||
*/
|
||||
export const degreesToRadians = (angleInDegrees: number) => {
|
||||
return (angleInDegrees * Math.PI) / 180;
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert a point object to an array of longitude,latitude
|
||||
* @param point
|
||||
*/
|
||||
export const pointToLongLatArray = (point: Point) => {
|
||||
return [point.longitude, point.latitude];
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a new point given a point, distance and bearing
|
||||
* https://www.igismap.com/formula-to-find-bearing-or-heading-angle-between-two-points-latitude-longitude/#:~:text=Here%20is%20the%20formula%20to,%E2%80%93%20sin%20la1%20*%20sin%20la2)
|
||||
*/
|
||||
export const getRelativePointByDistance = (
|
||||
point: Point,
|
||||
distance: number,
|
||||
bearing: number
|
||||
): Point => {
|
||||
const latitude = Math.asin(
|
||||
Math.sin(point.latitude) * Math.cos(distance) +
|
||||
Math.cos(point.latitude) * Math.sin(distance) * Math.cos(bearing)
|
||||
);
|
||||
const longitude =
|
||||
point.longitude +
|
||||
Math.atan2(
|
||||
Math.sin(bearing) * Math.sin(distance) * Math.cos(point.latitude),
|
||||
Math.cos(distance) - Math.sin(point.latitude) * Math.sin(latitude)
|
||||
);
|
||||
|
||||
return {
|
||||
latitude: radiansToDegrees(latitude),
|
||||
longitude: radiansToDegrees(longitude),
|
||||
};
|
||||
};
|
55
typescript/geo/radiusToPolygon/index.ts
Normal file
55
typescript/geo/radiusToPolygon/index.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import {
|
||||
degreesToRadians,
|
||||
getRelativePointByDistance,
|
||||
pointToLongLatArray,
|
||||
} from "./helpers";
|
||||
import { Point } from "./point";
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
const options = {
|
||||
/**
|
||||
* IERS Equatorial Radius of the earth
|
||||
* https://en.wikipedia.org/wiki/Earth_ellipsoid
|
||||
*/
|
||||
equatorialRadiusInMeters: 6378136.6,
|
||||
|
||||
/**
|
||||
* Number of edges to create in the polygon
|
||||
* Use 360 to get a true circle
|
||||
*/
|
||||
numberOfEdges: 64,
|
||||
};
|
||||
|
||||
const input = {
|
||||
center: { latitude: 51.7543453, longitude: 5.5526647 } as Point,
|
||||
radius: 2000,
|
||||
};
|
||||
|
||||
const coordinates = [];
|
||||
for (var i = 0; i < options.numberOfEdges; ++i) {
|
||||
const newPoint = getRelativePointByDistance(
|
||||
{
|
||||
latitude: degreesToRadians(input.center.latitude),
|
||||
longitude: degreesToRadians(input.center.longitude),
|
||||
},
|
||||
input.radius / options.equatorialRadiusInMeters,
|
||||
// find bearing https://www.igismap.com/formula-to-find-bearing-or-heading-angle-between-two-points-latitude-longitude/
|
||||
(2 * Math.PI * -i) / options.numberOfEdges
|
||||
);
|
||||
coordinates.push(pointToLongLatArray(newPoint));
|
||||
}
|
||||
// make sure the circle is closed by pushing a copy of the first element
|
||||
coordinates.push(coordinates[0]);
|
||||
|
||||
console.log(
|
||||
JSON.stringify(
|
||||
{
|
||||
type: "Polygon",
|
||||
coordinates: [coordinates],
|
||||
},
|
||||
null,
|
||||
2
|
||||
)
|
||||
);
|
4
typescript/geo/radiusToPolygon/point.ts
Normal file
4
typescript/geo/radiusToPolygon/point.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export interface Point {
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
}
|
213
typescript/inheritance-and-interfaces.ts
Normal file
213
typescript/inheritance-and-interfaces.ts
Normal file
@@ -0,0 +1,213 @@
|
||||
/**
|
||||
* interface which specifies all properties of a living being
|
||||
*/
|
||||
interface IlivingBeing {
|
||||
name: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface which specifies the bark method
|
||||
*/
|
||||
interface IBark {
|
||||
bark(): string; // functie die een string returned
|
||||
}
|
||||
|
||||
// You can't instantiate an abstract class so this class is merely used as a base
|
||||
abstract class animal implements IlivingBeing {
|
||||
name: string;
|
||||
age: number;
|
||||
|
||||
constructor(name: string, age: number) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
makeNoise() {
|
||||
return "No sound!";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The feline class extends the animal class, as such it gets all properties and methods of the animal class
|
||||
*/
|
||||
class feline extends animal {
|
||||
// we receive a name and age and pass it to the super class (animal)
|
||||
constructor(name: string, age: number) {
|
||||
super(name, age);
|
||||
}
|
||||
|
||||
// override the makeNoise method for felines
|
||||
makeNoise() {
|
||||
return "Miaauw";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The canine class extends animal but aditionally implements the bark interface
|
||||
* This means that we can put canine's in three groups now:
|
||||
* - objects that implement IBark
|
||||
* - Animals
|
||||
* - Canines
|
||||
*
|
||||
* we can mark canine as abstract because we have subdivided the canines into
|
||||
* dogs and wolves below.
|
||||
*/
|
||||
abstract class canine extends animal implements IBark {
|
||||
constructor(name: string, age: number) {
|
||||
super(name, age);
|
||||
}
|
||||
|
||||
makeNoise() {
|
||||
return this.bark();
|
||||
}
|
||||
|
||||
bark(): string {
|
||||
return "Woof not implemented";
|
||||
}
|
||||
|
||||
howl() {
|
||||
return "Hooooowwwllll!";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The dog class simply extends canine and overrides the bark method
|
||||
* Because dogs are happy creatures they can also wag their tails
|
||||
*/
|
||||
class dog extends canine {
|
||||
constructor(name: string, age: number) {
|
||||
super(name, age);
|
||||
}
|
||||
|
||||
wagTail() {
|
||||
return "happy";
|
||||
}
|
||||
|
||||
bark() {
|
||||
return "wooof";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Simply extends from canine and implements a louder bark (more o's in wooof)
|
||||
*/
|
||||
class wolf extends canine {
|
||||
constructor(name: string, age: number) {
|
||||
super(name, age);
|
||||
}
|
||||
|
||||
bark() {
|
||||
return "wooooof";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A rat is what we call those teeny tiny dogs (like chihuahuas)
|
||||
*/
|
||||
class rat extends canine {
|
||||
size: string;
|
||||
|
||||
constructor(name: string, age: number) {
|
||||
super(name, age);
|
||||
}
|
||||
|
||||
howl() {
|
||||
return "garble " + super.howl();
|
||||
}
|
||||
|
||||
bark() {
|
||||
return "woof";
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// The classes below show that we can even group "animals" and "others" together
|
||||
// if they all implement the same (ILivingBeing) interface
|
||||
//
|
||||
|
||||
class person implements IlivingBeing {
|
||||
name: string;
|
||||
|
||||
constructor(name: string) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Aliens don't really have a name (they have a designation, military race and all)
|
||||
* in order to class them as IlivingBeing we need to give them a name (because humans like to name everything)
|
||||
* we do this in the constructor by simply using the aliens designation as its name.
|
||||
*/
|
||||
class alien implements IlivingBeing {
|
||||
name: string;
|
||||
designation: number;
|
||||
|
||||
constructor(num: number) {
|
||||
this.designation = num;
|
||||
this.name = this.designation.toString();
|
||||
}
|
||||
}
|
||||
|
||||
// _______ _
|
||||
// |__ __| | |
|
||||
// | | ___ ___| |_ _ __ _ _ _ __ ___
|
||||
// | |/ _ \/ __| __| | '__| | | | '_ \/ __|
|
||||
// | | __/\__ \ |_ | | | |_| | | | \__ \
|
||||
// |_|\___||___/\__| |_| \__,_|_| |_|___/
|
||||
|
||||
// object declarations
|
||||
let cat1 = new feline("cat1", 6);
|
||||
let cat2 = new feline("cat2", 4);
|
||||
let dog1 = new dog("dog1", 12);
|
||||
let wolf1 = new wolf("wolf", 7);
|
||||
let dog2 = new dog("dog2", 4);
|
||||
let rat1 = new rat("rattekop", 6);
|
||||
let person1 = new person("John");
|
||||
let alien1 = new alien(12351);
|
||||
|
||||
// Arr declarations
|
||||
let animals: animal[] = [cat1, cat2, dog1, wolf1, dog2, rat1];
|
||||
let dogs: dog[] = [dog1, dog2];
|
||||
let canines: canine[] = [dog1, dog2, wolf1, rat1];
|
||||
let barkers: IBark[] = [...canines];
|
||||
let felines: feline[] = [cat1];
|
||||
let livingBeings: IlivingBeing[] = [
|
||||
cat1,
|
||||
cat2,
|
||||
dog1,
|
||||
wolf1,
|
||||
dog2,
|
||||
rat1,
|
||||
person1,
|
||||
alien1,
|
||||
];
|
||||
|
||||
// for loops
|
||||
console.log("Dog Wagtails");
|
||||
dogs.forEach((dog) => {
|
||||
console.log(dog.wagTail());
|
||||
});
|
||||
console.log(" ");
|
||||
|
||||
console.log("Canine Howls");
|
||||
canines.forEach((canine) => {
|
||||
console.log(canine.howl());
|
||||
});
|
||||
console.log(" ");
|
||||
|
||||
console.log("Animals makeNoise");
|
||||
animals.forEach((animal) => {
|
||||
console.log(animal.makeNoise());
|
||||
});
|
||||
console.log(" ");
|
||||
|
||||
console.log("Beings name");
|
||||
livingBeings.forEach((livingbeing) => {
|
||||
console.log(livingbeing.name, livingbeing);
|
||||
});
|
||||
console.log(" ");
|
||||
|
||||
console.log("Barkers");
|
||||
barkers.forEach((barker) => {
|
||||
console.log(barker.bark());
|
||||
});
|
Reference in New Issue
Block a user