##Problem 1: Transforming Coordinates in 2D Cartesian System

#python
import numpy as np

def transform_point(x, y):
    # Translation
    tx, ty = 2, -1
    x_translated = x + tx
    y_translated = y + ty

    # Rotation (90 degrees counterclockwise)
    angle = np.radians(90)
    cos_theta = np.cos(angle)
    sin_theta = np.sin(angle)
    x_rotated = x_translated * cos_theta - y_translated * sin_theta
    y_rotated = x_translated * sin_theta + y_translated * cos_theta

    # Scaling
    sx, sy = 2, 2
    x_scaled = x_rotated * sx
    y_scaled = y_rotated * sy

    return x_scaled, y_scaled

# Example point
x, y = 3, 4
x_transformed, y_transformed = transform_point(x, y)
print(f"Transformed point: ({x_transformed}, {y_transformed})")


##Problem 2: Converting Coordinates to Homogeneous Coordinates

#python
def convert_to_homogeneous(x, y):
    return x, y, 1

# Example point
x, y = 3, 4
homogeneous_point = convert_to_homogeneous(x, y)
print(f"Homogeneous coordinates: {homogeneous_point}")


##Problem 3: Mapping Coordinates from World to View Coordinate System

#python
def world_to_view(x_world, y_world, z_world, camera_position):
    x_camera, y_camera, z_camera = camera_position
    x_view = x_world - x_camera
    y_view = y_world - y_camera
    z_view = z_world - z_camera
    return x_view, y_view, z_view

# Example point and camera position
x_world, y_world, z_world = 2, 3, 4
camera_position = (0, 0, 0)
x_view, y_view, z_view = world_to_view(x_world, y_world, z_world, camera_position)
print(f"View coordinates: ({x_view}, {y_view}, {z_view})")


##Problem 4: Coordinate Mapping in Screen Space

#python
def ndc_to_screen(x_ndc, y_ndc, screen_width, screen_height):
    x_screen = int((x_ndc + 1) / 2 * screen_width)
    y_screen = int((1 - y_ndc) / 2 * screen_height)
    return x_screen, y_screen

# Example NDC point and screen dimensions
x_ndc, y_ndc = -0.5, 0.5
screen_width, screen_height = 800, 600
x_screen, y_screen = ndc_to_screen(x_ndc, y_ndc, screen_width, screen_height)
print(f"Screen coordinates: ({x_screen}, {y_screen})")


##Problem 5: Perspective Projection in Homogeneous Coordinates

#python
def perspective_projection(x_world, y_world, z_world, focal_length, screen_width, screen_height):
    # Convert to homogeneous coordinates
    x_homogeneous, y_homogeneous, z_homogeneous, w_homogeneous = x_world, y_world, z_world, 1

    # Apply perspective projection
    f = focal_length
    x_projected = x_homogeneous * f / z_homogeneous
    y_projected = y_homogeneous * f / z_homogeneous

    # Convert to NDC
    x_ndc = x_projected / z_world
    y_ndc = y_projected / z_world

    # Map to screen coordinates
    x_screen = int((x_ndc + 1) / 2 * screen_width)
    y_screen = int((1 - y_ndc) / 2 * screen_height)
    return x_screen, y_screen

# Example point and parameters
x_world, y_world, z_world = 3, 4, 5
focal_length = 2
screen_width, screen_height = 800, 600
x_screen, y_screen = perspective_projection(x_world, y_world, z_world, focal_length, screen_width, screen_height)
print(f"Screen coordinates after perspective projection: ({x_screen}, {y_screen})")
