fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication3
  7. {
  8.  
  9. class vec
  10. {
  11. public double x;
  12. public double y;
  13.  
  14. public vec(double x, double y)
  15. {
  16. this.x = x;
  17. this.y = y;
  18. }
  19.  
  20. public vec sub(vec vec1)
  21. {
  22. return new vec(this.x - vec1.x, this.y - vec1.y);
  23. }
  24.  
  25. public double magnitude()
  26. {
  27. return Math.Sqrt((this.x * this.x) + (this.y * this.y));
  28. }
  29.  
  30. public vec div(double scalar)
  31. {
  32. return new vec(this.x / scalar, this.y / scalar);
  33. }
  34.  
  35. public static double dot(vec vec1, vec vec2)
  36. {
  37. double ret = (vec1.x * vec2.x) + (vec1.y * vec2.y) ;
  38. return ret;
  39. }
  40. }
  41.  
  42. class Program
  43. {
  44. static void Main(string[] args)
  45. {
  46.  
  47. vec ball1pos = new vec(110,90);
  48. vec ball2pos = new vec(100,100);
  49. vec difference = ball2pos.sub(ball1pos);
  50. double distance = difference.magnitude();
  51. vec normal = difference.div(distance); // unit vector of pos delta
  52.  
  53. vec ball1vel = new vec(0, 2);
  54. vec ball2vel = new vec(0,-3);
  55. vec velocityDelta = ball2vel.sub(ball1vel);
  56. double velmag = velocityDelta.magnitude();
  57. vec velUnit = velocityDelta.div(velmag); // unit vector of vel delta
  58.  
  59. double dot = vec.dot(velUnit, normal); // dot product of unit vectors
  60. System.Console.Write(dot);
  61.  
  62. }
  63. }
  64. }
  65.  
Success #stdin #stdout 0.03s 33824KB
stdin
Standard input is empty
stdout
-0.707106781186547