fork download
  1. using System;
  2. using System.Numerics;
  3.  
  4. public class Test
  5. {
  6. static float LinearFormula(float x, Vector2 a, Vector2 b)
  7. {
  8. float slope = (b.Y - a.Y) / (b.X - a.X);
  9. float dx = x - a.X;
  10. float dy = slope * dx;
  11. return dy + a.Y;
  12. }
  13.  
  14. static float CossineFormula(float x)
  15. {
  16. return Math.Abs(x) - (float) Math.Cos(x * 3);
  17. }
  18.  
  19. static float MyFormula(float x, Vector2 a, Vector2 b)
  20. {
  21. return CossineFormula(x) - LinearFormula(x, a, b);
  22. }
  23.  
  24. static Vector2 FindIntersection(Vector2 a, Vector2 b)
  25. {
  26. float x1 = 2;
  27. float x2 = 3;
  28.  
  29. for (int i = 0; i < 100; i++)
  30. {
  31. float d = (x2 + x1) / 2;
  32. if (d == x1 || d == x2) break;
  33. float v3 = MyFormula(d, a, b);
  34. if (v3 > 0)
  35. {
  36. x2 = d;
  37. }
  38. else
  39. {
  40. x1 = d;
  41. }
  42. }
  43. return new Vector2(x1, CossineFormula(x1));
  44. }
  45.  
  46. public static void Main()
  47. {
  48. Console.WriteLine(FindIntersection(new Vector2(0, 4), new Vector2(1, 3)));
  49. }
  50. }
Success #stdin #stdout 0.02s 16572KB
stdin
Standard input is empty
stdout
<2.354912, 1.645088>