> For the complete documentation index, see [llms.txt](https://42-guide.gitbook.io/42-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://42-guide.gitbook.io/42-guide/core-curriculum/rank-02/fdf/isometric.md).

# Isometric

#### Step 1: `isometric` Function

The **`isometric`** function is used to convert 3D coordinates into 2D screen coordinates using an isometric projection. Isometric projection is a way to represent 3D objects in 2D, where the x, y, and z axes are equally spaced at a 120° angle, resulting in a rotated view that gives the illusion of depth.

**Code Explanation for `isometric`:**

```c
void isometric(int *x, int *y, int z)
{
    int tmp_x;
    int tmp_y;
    float angle;

    tmp_x = *x;  // Store the original x coordinate
    tmp_y = *y;  // Store the original y coordinate
    angle = 0.523599;  // This is 30 degrees in radians, used for the isometric projection

    // Apply the isometric transformation to the x and y coordinates
    *x = (tmp_x - tmp_y) * cos(angle);  // New x position in 2D is calculated
    *y = (tmp_x + tmp_y) * sin(angle) - z;  // New y position, subtracting z for the depth (height)
}
```

**Key Points:**

* **Isometric Transformation**: This formula rotates the 3D grid by 30 degrees (the `angle = 0.523599` is 30 degrees in radians) and projects the 3D coordinates into 2D.
* **`x` and `y` Calculation**:
  * The new **`x`** coordinate is calculated by subtracting the `y` coordinate from the `x` and then multiplying it by the cosine of the angle.
  * The new **`y`** coordinate is calculated by adding the `x` and `y` coordinates, then multiplying by the sine of the angle. The height (`z`) is subtracted from the result to simulate the depth in the 3D world.
* **Effect**: This creates a 2D projection of the 3D map, making it look like it's rotated for a "top-down" view that gives the illusion of depth and height.

***

#### Step 2: `reproduce_pixels` Function

The **`reproduce_pixels`** function is used to scale and adjust the pixel positions based on the window size and zoom factor. It ensures the map fits inside the window, considering any zoom and scaling.

**Code Explanation for `reproduce_pixels`:**

```c
void reproduce_pixels(int *x1, int *x2, int *y1, int *y2, fdf *data)
{
    int max_x = data->map.width;  // Get the width of the map
    int max_y = data->map.height; // Get the height of the map
    int auto_scale;               // Auto scaling factor to keep the map within window bounds
    int max_dimension = (max_x > max_y) ? max_x : max_y;  // Find the largest dimension (width or height)
    
    // Calculate scale factors for both axes based on window size
    float scale_factor_x = (WIN_WIDTH * 0.4) / max_dimension;
    float scale_factor_y = (WIN_HEIGHT * 0.4) / max_dimension;
    
    // Choose the smaller scale factor to maintain aspect ratio
    auto_scale = (scale_factor_x < scale_factor_y) ? scale_factor_x : scale_factor_y;

    // Apply scaling to the coordinates and depth (z) values
    *x1 *= auto_scale + data->window.zoom;
    *y1 *= auto_scale + data->window.zoom;
    *x2 *= auto_scale + data->window.zoom;
    *y2 *= auto_scale + data->window.zoom;
    
    // Scale depth values (z1 and z2) by zoom factor and isometric adjustment
    data->side.z1 *= auto_scale + data->window.zoom + data->side.iso;
    data->side.z2 *= auto_scale + data->window.zoom + data->side.iso;
}
```

**Key Points:**

* **Auto Scaling**:
  * This part of the code calculates the scale factor to ensure that the map fits within the window. The scale factor is determined by dividing the width of the window by the largest dimension of the map (width or height).
  * The smaller scale factor (either `scale_factor_x` or `scale_factor_y`) is used to maintain the correct aspect ratio.
* **Zoom**: The `zoom` factor from the window settings is added to the scaling to allow for zooming in or out of the map.
* **Depth Scaling**: The `z1` and `z2` depth values (height values of points) are also scaled, considering the zoom and the isometric transformation. This ensures that the depth effect stays consistent with the scaled map.

***

#### Step 3: `locate` Function

The **`locate`** function adjusts the position of the map on the screen, ensuring it's centered and properly aligned based on the screen size, zoom level, and optional shifting.

**Code Explanation for `locate`:**

```c
void locate(int *x1, int *x2, int *y1, int *y2, fdf *data)
{
    // Center the map horizontally by calculating the shift based on zoom and map size
    *x1 += (WIN_WIDTH / 2) - (data->map.width * data->window.zoom / 2) + data->window.shift_left;
    *y1 += (WIN_HEIGHT / 8) - (data->map.height * data->window.zoom / 2) + data->window.shift_up;

    // Do the same for the second point (x2, y2)
    *x2 += (WIN_WIDTH / 2) - (data->map.width * data->window.zoom / 2) + data->window.shift_right;
    *y2 += (WIN_HEIGHT / 8) - (data->map.height * data->window.zoom / 2) + data->window.shift_down;
}
```

**Key Points:**

* **Centering the Map**:
  * The map is centered horizontally by adjusting the `x1` and `x2` coordinates. The center of the window is calculated as `WIN_WIDTH / 2`, and the map's width is adjusted based on the zoom factor.
  * Similarly, for vertical centering, the `y1` and `y2` coordinates are adjusted to ensure that the map is correctly positioned in the window.
* **Shifting**: The function also accounts for optional shifting (like `shift_left`, `shift_up`, etc.) that allows the user to move the map around within the window, making it more interactive.

***

#### Step 4: `set_pixels` Function

The **`set_pixels`** function is responsible for clearing the screen, drawing the map, and displaying the image. It manages the image rendering process.

**Code Explanation for `set_pixels`:**

```c
void set_pixels(fdf *data)
{
    // Clear the image buffer (set all pixels to zero)
    ft_bzero(data->address_data, WIN_WIDTH * WIN_HEIGHT * 4);
    
    // Clear the window before drawing the new image
    mlx_clear_window(data->mlx, data->wnd);

    // Optionally, draw usage (e.g., for debugging or additional info)
    // draw_usage(data);

    // Draw the map based on current data
    draw_map(data);

    // Put the drawn image onto the window
    mlx_put_image_to_window(data->mlx, data->wnd, data->img, 0, 0);
}
```

**Key Points:**

* **Clearing the Buffer**: The `ft_bzero` function clears the pixel buffer by setting all pixels to zero. This is important for ensuring that the screen is reset before the new frame is drawn.
* **Drawing the Map**: The `draw_map` function is called to actually draw the map based on the current data (coordinates, colors, etc.).
* **Displaying the Image**: Finally, `mlx_put_image_to_window` is used to display the image on the window. It places the rendered image at the specified position (`0, 0` in this case).

***

#### Conclusion

1. **Isometric Projection**: The `isometric` function rotates and projects the 3D coordinates into 2D for an isometric view.
2. **Pixel Reproduction & Scaling**: The `reproduce_pixels` function scales the map and adjusts the coordinates and depth values based on the window size and zoom level.
3. **Map Centering**: The `locate` function ensures the map is centered and shifted as per the user's settings.
4. **Pixel Drawing**: The `set_pixels` function handles clearing the screen, drawing the map, and displaying the final image.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://42-guide.gitbook.io/42-guide/core-curriculum/rank-02/fdf/isometric.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
