Skip to content

Commit

Permalink
draw: make sure we stay in bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
arrufat committed Aug 13, 2024
1 parent b5a063e commit 7574d93
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/draw.zig
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub fn drawLine(comptime T: type, image: Image(T), p1: Point2d, p2: Point2d, wid
if (y >= 0 and y <= rows - 1) {
var j = -half_width;
while (j <= half_width) : (j += 1) {
const py = y + j;
const py = @max(0, y + j);
const pos = as(usize, py) * image.cols + as(usize, x);
if (py >= 0 and py < rows) {
var c1: Rgba = Color.convert(Rgba, image.data[pos]);
Expand All @@ -95,7 +95,7 @@ pub fn drawLine(comptime T: type, image: Image(T), p1: Point2d, p2: Point2d, wid
if (y + 1 >= 0 and y + 1 <= rows - 1) {
var j = -half_width;
while (j <= half_width) : (j += 1) {
const py = y + 1 + j;
const py = @max(0, y + 1 + j);
if (py >= 0 and py < rows) {
const pos = as(usize, py) * image.cols + as(usize, x);
var c1: Rgba = Color.convert(Rgba, image.data[pos]);
Expand Down Expand Up @@ -124,7 +124,7 @@ pub fn drawLine(comptime T: type, image: Image(T), p1: Point2d, p2: Point2d, wid
if (x >= 0 and x <= cols - 1) {
var j = -half_width;
while (j <= half_width) : (j += 1) {
const px = x + j;
const px = @max(0, x + j);
const pos = as(usize, y) * image.cols + as(usize, px);
if (px >= 0 and px < cols) {
var c1: Rgba = Color.convert(Rgba, image.data[pos]);
Expand All @@ -143,7 +143,7 @@ pub fn drawLine(comptime T: type, image: Image(T), p1: Point2d, p2: Point2d, wid
c2.a = @intFromFloat((dx - x) * max_alpha);
var j = -half_width;
while (j <= half_width) : (j += 1) {
const px = x + 1 + j;
const px = @max(0, x + 1 + j);
const pos = as(usize, y) * image.cols + as(usize, px);
if (px >= 0 and px < cols) {
var c1: Rgba = Color.convert(Rgba, image.data[pos]);
Expand Down

0 comments on commit 7574d93

Please sign in to comment.