fork download
  1. # your code goes here
  2. __author__ = 'sidharthgoyal'
  3. import math
  4.  
  5. increment = 0.1
  6. startingPoint = [1, 1]
  7. point1 = [1,5]
  8. point2 = [6,4]
  9. point3 = [5,2]
  10. point4 = [2,1]
  11.  
  12. def distance(x1, y1, x2, y2):
  13. dist = math.pow(x2-x1, 2) + math.pow(y2-y1, 2)
  14. return dist
  15.  
  16. def sumOfDistances(x1, y1, px1, py1, px2, py2, px3, py3, px4, py4):
  17. d1 = distance(x1, y1, px1, py1)
  18. d2 = distance(x1, y1, px2, py2)
  19. d3 = distance(x1, y1, px3, py3)
  20. d4 = distance(x1, y1, px4, py4)
  21.  
  22. return d1 + d2 + d3 + d4
  23.  
  24. def newDistance(x1, y1, point1, point2, point3, point4):
  25. d1 = [x1, y1]
  26. d1temp = sumOfDistances(x1, y1, point1[0],point1[1], point2[0],point2[1],
  27. point3[0],point3[1], point4[0],point4[1] )
  28. d1.append(d1temp)
  29. return d1
  30.  
  31. minDistance = sumOfDistances(startingPoint[0], startingPoint[1], point1[0],point1[1], point2[0],point2[1],
  32. point3[0],point3[1], point4[0],point4[1] )
  33. flag = True
  34.  
  35. def newPoints(minimum, d1, d2, d3, d4):
  36. if d1[2] == minimum:
  37. return [d1[0], d1[1]]
  38. elif d2[2] == minimum:
  39. return [d2[0], d2[1]]
  40. elif d3[2] == minimum:
  41. return [d3[0], d3[1]]
  42. elif d4[2] == minimum:
  43. return [d4[0], d4[1]]
  44.  
  45. i = 1
  46. while flag:
  47. d1 = newDistance(startingPoint[0]+increment, startingPoint[1], point1, point2, point3, point4)
  48. d2 = newDistance(startingPoint[0]-increment, startingPoint[1], point1, point2, point3, point4)
  49. d3 = newDistance(startingPoint[0], startingPoint[1]+increment, point1, point2, point3, point4)
  50. d4 = newDistance(startingPoint[0], startingPoint[1]-increment, point1, point2, point3, point4)
  51. print i,' ', round(startingPoint[0], 2), round(startingPoint[1], 2)
  52. minimum = min(d1[2], d2[2], d3[2], d4[2])
  53. if minimum < minDistance:
  54. startingPoint = newPoints(minimum, d1, d2, d3, d4)
  55. minDistance = minimum
  56. #print i,' ', round(startingPoint[0], 2), round(startingPoint[1], 2)
  57. i+=1
  58. else:
  59. flag = False
  60.  
Success #stdin #stdout 0.01s 7372KB
stdin
Standard input is empty
stdout
1   1.0 1.0
2   1.1 1.0
3   1.2 1.0
4   1.3 1.0
5   1.4 1.0
6   1.5 1.0
7   1.6 1.0
8   1.6 1.1
9   1.7 1.1
10   1.7 1.2
11   1.7 1.3
12   1.8 1.3
13   1.8 1.4
14   1.9 1.4
15   2.0 1.4
16   2.0 1.5
17   2.1 1.5
18   2.1 1.6
19   2.2 1.6
20   2.2 1.7
21   2.3 1.7
22   2.3 1.8
23   2.3 1.9
24   2.4 1.9
25   2.5 1.9
26   2.5 2.0
27   2.6 2.0
28   2.6 2.1
29   2.7 2.1
30   2.7 2.2
31   2.8 2.2
32   2.8 2.3
33   2.9 2.3
34   2.9 2.4
35   3.0 2.4
36   3.0 2.5
37   3.1 2.5
38   3.1 2.6
39   3.2 2.6
40   3.2 2.7
41   3.2 2.8
42   3.3 2.8
43   3.4 2.8
44   3.4 2.9
45   3.5 2.9
46   3.5 3.0