Point.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. export class Point {
  2. constructor(x, y){
  3. this.x = x;
  4. this.y = y;
  5. }
  6. clone(point) {
  7. this.x = point.x
  8. this.y = point.y
  9. return this
  10. }
  11. scale(x, y) {
  12. this.x *= x;
  13. this.y *= y;
  14. return this
  15. }
  16. difference(point){
  17. return new Point(this.x - point.x, this.y - point.y)
  18. }
  19. equals(point) {
  20. return this.x == point.x && this.y == point.y
  21. }
  22. offset(point) {
  23. this.x += point.x
  24. this.y += point.y
  25. return this
  26. }
  27. angleTo(point) {
  28. return Math.atan2(point.y - this.y, point.x - this.x)
  29. }
  30. vectorTo(point) {
  31. let angle = this.angleTo(point)
  32. return new Point(Math.cos(angle), Math.sin(angle))
  33. }
  34. distanceTo(point) {
  35. return Math.hypot(point.y - this.y, point.x - this.x)
  36. }
  37. roundTo(point, threshholdX, threshholdY) {
  38. let diff = this.difference(point)
  39. if(Math.abs(diff.x) <= threshholdX) {
  40. this.x = point.x
  41. }
  42. if(Math.abs(diff.y) <= threshholdY) {
  43. this.y = point.y
  44. }
  45. }
  46. }