convert SDL_Renderer circle & arcs to float maths

This commit is contained in:
2024-03-10 20:35:08 +11:00
parent 8a5529e752
commit 123d228e14
2 changed files with 19 additions and 18 deletions

View File

@ -59,18 +59,18 @@ void DrawLine(int x1, int y1, int x2, int y2)
void DrawCircleSteps(int x, int y, int r, int steps) void DrawCircleSteps(int x, int y, int r, int steps)
{ {
double stepsz = (double)TAU / steps; const float stepsz = (float)TAU / steps;
int lastx = r; const float mag = (float)r;
int lasty = 0; float lastx = mag;
float lasty = 0;
for (int i = 1; i <= steps; ++i) for (int i = 1; i <= steps; ++i)
{ {
const double mag = (double)r; float ofsx = cosf(stepsz * i) * mag;
int ofsx = (int)round(cos(stepsz * i) * mag); float ofsy = sinf(stepsz * i) * mag;
int ofsy = (int)round(sin(stepsz * i) * mag);
SDL_RenderLine(rend, SDL_RenderLine(rend,
(float)(x + lastx), (float)(y + lasty), x + lastx, y + lasty,
(float)(x + ofsx), (float)(y + ofsy)); x + ofsx, y + ofsy);
lastx = ofsx; lastx = ofsx;
lasty = ofsy; lasty = ofsy;
@ -79,21 +79,21 @@ void DrawCircleSteps(int x, int y, int r, int steps)
void DrawArcSteps(int x, int y, int r, int startAng, int endAng, int steps) void DrawArcSteps(int x, int y, int r, int startAng, int endAng, int steps)
{ {
const double fstart = (double)startAng * DEG2RAD; const float fstart = (float)startAng * (float)DEG2RAD;
const double fstepSz = (double)(endAng - startAng) / abs(steps) * DEG2RAD; const float fstepSz = (float)(endAng - startAng) / abs(steps) * (float)DEG2RAD;
const double mag = (double)r; const float mag = (float)r;
int lastx = (int)round(cos(fstart) * mag); float lastx = cosf(fstart) * mag;
int lasty = (int)round(-sin(fstart) * mag); float lasty = -sinf(fstart) * mag;
for (int i = 1; i <= steps; ++i) for (int i = 1; i <= steps; ++i)
{ {
const double theta = fstart + fstepSz * (double)i; const float theta = fstart + fstepSz * (float)i;
int ofsx = (int)round(cos(theta) * mag); float ofsx = cosf(theta) * mag;
int ofsy = (int)round(-sin(theta) * mag); float ofsy = -sinf(theta) * mag;
SDL_RenderLine(rend, SDL_RenderLine(rend,
(float)(x + lastx), (float)(y + lasty), x + lastx, y + lasty,
(float)(x + ofsx), (float)(y + ofsy)); x + ofsx, y + ofsy);
lastx = ofsx; lastx = ofsx;
lasty = ofsy; lasty = ofsy;

View File

@ -2,6 +2,7 @@
#include "maths.h" #include "maths.h"
#include <stdlib.h> #include <stdlib.h>
void DrawCircle(int x, int y, int r) void DrawCircle(int x, int y, int r)
{ {
const int steps = (int)(sqrt((double)r) * 8.0); const int steps = (int)(sqrt((double)r) * 8.0);