Repository: alacritty/alacritty
File: alacritty/src/renderer/text/builtin_font.rs (Rust)

Code before:
        }
    }

    /// Draws a quarter of a circle centered in `(0., self.height - radius)` with radius
    /// `self.width` and an attached rectangle to form a "╭" using a given `stroke_size` in the
    /// bottom-right quadrant of the `Canvas` coordinate system.
    fn draw_rounded_corner(&mut self, stroke_size: usize) {
        let r = 0.5 * (self.width.min(self.height) + stroke_size) as f32;
        let w = stroke_size as f32;

        let mut x_offset = 0.0;
        let mut y_offset = 0.0;
        let d_bias: f32;
        if self.height > self.width {
            y_offset = self.height as f32 * 0.5 - r + w * 0.5;
            if (self.width % 2 != self.height % 2) && (self.height % 2 == stroke_size % 2) {
                y_offset += 1.0;
            }
            d_bias = if self.width % 2 == stroke_size % 2 { 0.0 } else { 0.5 };
        } else {
            x_offset = self.width as f32 * 0.5 - r + w * 0.5;
            if (self.width % 2 != self.height % 2) && (self.width % 2 == stroke_size % 2) {
                x_offset += 1.0;
            }
            d_bias = if self.height % 2 == stroke_size % 2 { 0.0 } else { 0.5 };
        }

        let radius_i = (self.width.min(self.height) + stroke_size + 1) / 2;
        for y in 0..radius_i {
            for x in 0..radius_i {
                let y = y as f32;
                let x = x as f32;
                let d = (x * x + y * y).sqrt() + d_bias;
                let v = if d < r - w - 1.0 {
                    0.0
                } else if d < r - w {
                    1.0 + d - (r - w)
                } else if d < r - 1.0 {
                    1.0
                } else if d < r {
                    r - d
                } else {
                    0.0
                };
                self.put_pixel(
                    x + x_offset,
                    y + y_offset,
                    Pixel::gray((COLOR_FILL._r as f32 * v) as u8),
                );
            }
        }

Review comment (suggestion):
This entire thing can be rewritten in terms of bigger side, since calculations are the same through out. You'd just need to e.g. `if width > height { (&self.width, &mut x_offset) else { (&self.height, &mut y_offset) }`.

Code after:
        }
    }

    /// Draws a quarter of a circle centered in `(0., self.height - radius)` with radius
    /// `self.width` and an attached rectangle to form a "╭" using a given `stroke_size` in the
    /// bottom-right quadrant of the `Canvas` coordinate system.
    fn draw_rounded_corner(&mut self, stroke_size: usize) {
        let radius = (self.width.min(self.height) + stroke_size) as f32 / 2.;
        let stroke_size_f = stroke_size as f32;

        let mut x_offset = 0.;
        let mut y_offset = 0.;
        let (long_side, short_side, offset) = if self.height > self.width {
            (&self.height, &self.width, &mut y_offset)
        } else {
            (&self.width, &self.height, &mut x_offset)
        };
        let distance_bias = if short_side % 2 == stroke_size % 2 { 0. } else { 0.5 };
        *offset = *long_side as f32 / 2. - radius + stroke_size_f / 2.;
        if (self.width % 2 != self.height % 2) && (long_side % 2 == stroke_size % 2) {
            *offset += 1.;
        }

        let radius_i = (short_side + stroke_size + 1) / 2;
        for y in 0..radius_i {
            for x in 0..radius_i {
                let y = y as f32;
                let x = x as f32;
                let distance = x.hypot(y) + distance_bias;
                let value = if distance < radius - stroke_size_f - 1. {
                    // Inside the circle.
                    0.
                } else if distance < radius - stroke_size_f {
                    // On the inner border.
                    1. + distance - (radius - stroke_size_f)
                } else if distance < radius - 1. {
                    // Inside the stroke.
                    1.
                } else if distance < radius {
                    // On the outer border.
                    radius - distance
                } else {
                    // Outside of the circle.
                    0.
                };

                self.put_pixel(
                    x + x_offset,
                    y + y_offset,
                    Pixel::gray((COLOR_FILL._r as f32 * value) as u8),
                );