fork download
  1. ##Problem 1: Transforming Coordinates in 2D Cartesian System
  2.  
  3. #python
  4. import numpy as np
  5.  
  6. def transform_point(x, y):
  7. # Translation
  8. tx, ty = 2, -1
  9. x_translated = x + tx
  10. y_translated = y + ty
  11.  
  12. # Rotation (90 degrees counterclockwise)
  13. angle = np.radians(90)
  14. cos_theta = np.cos(angle)
  15. sin_theta = np.sin(angle)
  16. x_rotated = x_translated * cos_theta - y_translated * sin_theta
  17. y_rotated = x_translated * sin_theta + y_translated * cos_theta
  18.  
  19. # Scaling
  20. sx, sy = 2, 2
  21. x_scaled = x_rotated * sx
  22. y_scaled = y_rotated * sy
  23.  
  24. return x_scaled, y_scaled
  25.  
  26. # Example point
  27. x, y = 3, 4
  28. x_transformed, y_transformed = transform_point(x, y)
  29. print(f"Transformed point: ({x_transformed}, {y_transformed})")
  30.  
  31.  
  32. ##Problem 2: Converting Coordinates to Homogeneous Coordinates
  33.  
  34. #python
  35. def convert_to_homogeneous(x, y):
  36. return x, y, 1
  37.  
  38. # Example point
  39. x, y = 3, 4
  40. homogeneous_point = convert_to_homogeneous(x, y)
  41. print(f"Homogeneous coordinates: {homogeneous_point}")
  42.  
  43.  
  44. ##Problem 3: Mapping Coordinates from World to View Coordinate System
  45.  
  46. #python
  47. def world_to_view(x_world, y_world, z_world, camera_position):
  48. x_camera, y_camera, z_camera = camera_position
  49. x_view = x_world - x_camera
  50. y_view = y_world - y_camera
  51. z_view = z_world - z_camera
  52. return x_view, y_view, z_view
  53.  
  54. # Example point and camera position
  55. x_world, y_world, z_world = 2, 3, 4
  56. camera_position = (0, 0, 0)
  57. x_view, y_view, z_view = world_to_view(x_world, y_world, z_world, camera_position)
  58. print(f"View coordinates: ({x_view}, {y_view}, {z_view})")
  59.  
  60.  
  61. ##Problem 4: Coordinate Mapping in Screen Space
  62.  
  63. #python
  64. def ndc_to_screen(x_ndc, y_ndc, screen_width, screen_height):
  65. x_screen = int((x_ndc + 1) / 2 * screen_width)
  66. y_screen = int((1 - y_ndc) / 2 * screen_height)
  67. return x_screen, y_screen
  68.  
  69. # Example NDC point and screen dimensions
  70. x_ndc, y_ndc = -0.5, 0.5
  71. screen_width, screen_height = 800, 600
  72. x_screen, y_screen = ndc_to_screen(x_ndc, y_ndc, screen_width, screen_height)
  73. print(f"Screen coordinates: ({x_screen}, {y_screen})")
  74.  
  75.  
  76. ##Problem 5: Perspective Projection in Homogeneous Coordinates
  77.  
  78. #python
  79. def perspective_projection(x_world, y_world, z_world, focal_length, screen_width, screen_height):
  80. # Convert to homogeneous coordinates
  81. x_homogeneous, y_homogeneous, z_homogeneous, w_homogeneous = x_world, y_world, z_world, 1
  82.  
  83. # Apply perspective projection
  84. f = focal_length
  85. x_projected = x_homogeneous * f / z_homogeneous
  86. y_projected = y_homogeneous * f / z_homogeneous
  87.  
  88. # Convert to NDC
  89. x_ndc = x_projected / z_world
  90. y_ndc = y_projected / z_world
  91.  
  92. # Map to screen coordinates
  93. x_screen = int((x_ndc + 1) / 2 * screen_width)
  94. y_screen = int((1 - y_ndc) / 2 * screen_height)
  95. return x_screen, y_screen
  96.  
  97. # Example point and parameters
  98. x_world, y_world, z_world = 3, 4, 5
  99. focal_length = 2
  100. screen_width, screen_height = 800, 600
  101. x_screen, y_screen = perspective_projection(x_world, y_world, z_world, focal_length, screen_width, screen_height)
  102. print(f"Screen coordinates after perspective projection: ({x_screen}, {y_screen})")
  103.  
Success #stdin #stdout 0.99s 41452KB
stdin
Standard input is empty
stdout
Transformed point: (-5.999999999999999, 10.0)
Homogeneous coordinates: (3, 4, 1)
View coordinates: (2, 3, 4)
Screen coordinates: (200, 150)
Screen coordinates after perspective projection: (496, 203)