Draw line between 2 point in Texture2D

I have 2 screen coordinates:

Vector2Int origin;
Vector2Int end;
Texture2D img;
Drawline(origin, end, Color.red, img);

private void DrawLine(Vector2Int origin, Vector2Int end, Color color, Texture2D targetTexture){
            int diffX = (int)end.X - (int)origin.X;
            int diffY = (int)end.Y - (int)origin.Y;
            float error = 0;

            float diffError = Math.Abs((float)diffY / (float)diffX);
           
            int y = (int)origin.Y;
            for (int x = (int)origin.X;x<endpoint.X;x++)
            {
                print(x + " " + y);
                targetTexture.SetPixel(x, y, Color.Red);
                error = error + diffError;
                if (error >= 0.5)
                {
                    ++y;
                    error -= 1f;
                }
            }
            targetTexture.Apply();
}

I get an error saying that the Y from the SetPixel method is greater than the texture’s height. The texture is 640x400 and the Vector2Int origin and Vector2Int end coordinates are not greater than the image, however the method I wrote generates some Y coordinates greater than 400.

Detect it and either a) clamp it (Mathf.Clamp()), or b) don’t set it.

I clamped it and it now works. Thanks!