F1 circuit layouts with year-by-year SVGs — manually traced track variations
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

57 rindas
1.9KB

  1. from dataclasses import dataclass
  2. from typing import List, Dict, Any
  3. import numpy as np
  4. @dataclass
  5. class GeoJSONGeometry:
  6. type: str
  7. coordinates: List[tuple[float, float]]
  8. @classmethod
  9. def from_dict(cls, data: Dict[str, Any]) -> 'GeoJSONGeometry':
  10. return cls(
  11. type=data['type'],
  12. coordinates=data['coordinates']
  13. )
  14. def find_longest_straight(self, window_size=5) -> (List[float], List[float]):
  15. """Find the longest approximately straight section of the track, including wrap-around."""
  16. max_distance = 0
  17. best_start_idx = 0
  18. n = len(self.coordinates)
  19. # Helper function to check straightness
  20. def is_straight(points, start, end, length):
  21. direction = (end - start) / length
  22. distances = []
  23. for point in points:
  24. projection = start + np.dot(point - start, direction) * direction
  25. distance = np.linalg.norm(point - projection)
  26. distances.append(distance)
  27. return max(distances) < length * 0.05 # 5% tolerance
  28. # Check all possible segments, including wrap-around
  29. for i in range(n):
  30. # Get window_size points, handling wrap-around
  31. segment = []
  32. for j in range(window_size):
  33. idx = (i + j) % n
  34. segment.append(np.array(self.coordinates[idx]))
  35. start = np.array(segment[0])
  36. end = np.array(segment[-1])
  37. length = np.linalg.norm(end - start)
  38. if length > max_distance:
  39. # Check if all points are roughly on the line
  40. if is_straight(segment, start, end, length):
  41. max_distance = length
  42. best_start_idx = i
  43. # Return indices that might wrap around
  44. end_idx = (best_start_idx + window_size) % len(self.coordinates)
  45. return self.coordinates[best_start_idx], self.coordinates[end_idx]