Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. function hexToRgb(hex) {
  2. // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
  3. var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
  4. hex = hex.replace(shorthandRegex, function(m, r, g, b) {
  5. return r + r + g + g + b + b;
  6. });
  7.  
  8. var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  9. return result ? {
  10. r: parseInt(result[1], 16),
  11. g: parseInt(result[2], 16),
  12. b: parseInt(result[3], 16)
  13. } : null;
  14. }
  15.  
  16. class Entity {
  17. constructor(_color, _radius, _x, _y, col) {
  18. this.color = _color;
  19. this.radius = _radius;
  20. this.x = _x;
  21. this.y = _y;
  22. this.collides = true;
  23. }
  24.  
  25. draw() {
  26. fill(hexToRgb(this.color).r,
  27. hexToRgb(this.color).g,
  28. hexToRgb(this.color).b);
  29. ellipse(this.x, this.y, this.radius, this.radius);
  30.  
  31. }
  32. }
  33.  
  34. foo = new Entity("#000000", 40, 300, 300);
  35. let collider;
  36.  
  37. function setup() {
  38. createCanvas(400, 400);
  39. background(255, 255, 255);
  40. fill(0, 0, 0);
  41. collider = new Entity("#FF0000", 60, width/2, height/2);
  42. }
  43.  
  44. function draw() {
  45. background(255, 255, 255);
  46. foo.x = mouseX;
  47. foo.y = mouseY;
  48.  
  49. if (pythagoras(foo.x, collider.x, foo.y, collider.y) < (collider.radius/2 + foo.radius/2)) {
  50. foo.color = "#FF0000";
  51. }
  52. else {
  53. foo.color = "#000000";
  54. }
  55. collider.draw();
  56. foo.draw();
  57. }
  58.  
  59. function pythagoras (x1, x2, y1, y2) {
  60. toRet = sqrt(pow(x2 - x1, 2) + pow ((y2 - y1), 2));
  61. console.log(toRet);
  62. return toRet;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement