From c2fe5fa196b35b75e4069c70dd74c2b1afc3d7a5 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Tue, 24 Aug 2021 22:04:53 +0900 Subject: [PATCH] graphics: const arguments for point/line calculations --- base/usr/include/toaru/graphics.h | 12 ++++++------ lib/graphics.c | 17 +++++++++++------ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/base/usr/include/toaru/graphics.h b/base/usr/include/toaru/graphics.h index 2b9e0d98..9690804f 100644 --- a/base/usr/include/toaru/graphics.h +++ b/base/usr/include/toaru/graphics.h @@ -109,12 +109,12 @@ struct gfx_point { float y; }; -extern float gfx_point_distance(struct gfx_point * a, struct gfx_point * b); -extern float gfx_point_distance_squared(struct gfx_point * a, struct gfx_point * b); -extern float gfx_point_dot(struct gfx_point * a, struct gfx_point * b); -extern struct gfx_point gfx_point_sub(struct gfx_point * a, struct gfx_point * b); -extern struct gfx_point gfx_point_add(struct gfx_point * a, struct gfx_point * b); -extern float gfx_line_distance(struct gfx_point * p, struct gfx_point * v, struct gfx_point * w); +extern float gfx_point_distance(const struct gfx_point * a, const struct gfx_point * b); +extern float gfx_point_distance_squared(const struct gfx_point * a, const struct gfx_point * b); +extern float gfx_point_dot(const struct gfx_point * a, const struct gfx_point * b); +extern struct gfx_point gfx_point_sub(const struct gfx_point * a, const struct gfx_point * b); +extern struct gfx_point gfx_point_add(const struct gfx_point * a, const struct gfx_point * b); +extern float gfx_line_distance(const struct gfx_point * p, const struct gfx_point * v, const struct gfx_point * w); extern void draw_line_aa(gfx_context_t * ctx, int x_1, int x_2, int y_1, int y_2, uint32_t color, float thickness); struct gradient_definition { diff --git a/lib/graphics.c b/lib/graphics.c index c9ebb3ec..42211a3b 100644 --- a/lib/graphics.c +++ b/lib/graphics.c @@ -1038,7 +1038,7 @@ uint32_t gfx_vertical_gradient_pattern(int32_t x, int32_t y, double alpha, void alpha * 255)); } -float gfx_point_distance(struct gfx_point * a, struct gfx_point * b) { +float gfx_point_distance(const struct gfx_point * a, const struct gfx_point * b) { return sqrt((a->x - b->x) * (a->x - b->x) + (a->y - b->y) * (a->y - b->y)); } @@ -1054,7 +1054,12 @@ void draw_rounded_rectangle_pattern(gfx_context_t * ctx, int32_t x, int32_t y, u } for (int row = y; row < y + height; row++){ + if (row < 0) continue; + if (row >= ctx->height) break; for (int col = x; col < x + width; col++) { + if (col < 0) continue; + if (col >= ctx->width) break; + if ((col < x + radius || col > x + width - radius - 1) && (row < y + radius || row > y + height - radius - 1)) { continue; @@ -1097,25 +1102,25 @@ void draw_rounded_rectangle(gfx_context_t * ctx, int32_t x, int32_t y, uint16_t draw_rounded_rectangle_pattern(ctx,x,y,width,height,radius,gfx_fill_pattern,&color); } -float gfx_point_distance_squared(struct gfx_point * a, struct gfx_point * b) { +float gfx_point_distance_squared(const struct gfx_point * a, const struct gfx_point * b) { return (a->x - b->x) * (a->x - b->x) + (a->y - b->y) * (a->y - b->y); } -float gfx_point_dot(struct gfx_point * a, struct gfx_point * b) { +float gfx_point_dot(const struct gfx_point * a, const struct gfx_point * b) { return (a->x * b->x) + (a->y * b->y); } -struct gfx_point gfx_point_sub(struct gfx_point * a, struct gfx_point * b) { +struct gfx_point gfx_point_sub(const struct gfx_point * a, const struct gfx_point * b) { struct gfx_point p = {a->x - b->x, a->y - b->y}; return p; } -struct gfx_point gfx_point_add(struct gfx_point * a, struct gfx_point * b) { +struct gfx_point gfx_point_add(const struct gfx_point * a, const struct gfx_point * b) { struct gfx_point p = {a->x + b->x, a->y + b->y}; return p; } -float gfx_line_distance(struct gfx_point * p, struct gfx_point * v, struct gfx_point * w) { +float gfx_line_distance(const struct gfx_point * p, const struct gfx_point * v, const struct gfx_point * w) { float lengthlength = gfx_point_distance_squared(v,w); if (lengthlength == 0.0) return gfx_point_distance(p, v); /* point */