QB64 Phoenix Edition
Playing with code: a circle function using triangle math - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: QBJS, BAM, and Other BASICs (https://qb64phoenix.com/forum/forumdisplay.php?fid=50)
+--- Thread: Playing with code: a circle function using triangle math (/showthread.php?tid=1995)



Playing with code: a circle function using triangle math - CharlieJV - 09-13-2023

Just experimenting with some code for creating circles with "triangle math".

As always, I start with big and ugly code just to understand the pieces before making the code pretty.

Code: (Select All)
' C is the right angle
' c is opposite side
' c squared = a squared + b squared

size = 100
aspect_ratio = 1
screen _newimage(size, size, 12)

root_x = size / 2 : root_y = size / 2
c = size / 2 - 25

for a = C to 0 step - 0.01
b = ( c ^ 2 - a ^ 2 ) ^ (0.5)
pset (root_x + a, root_y - (b/aspect_ratio)), 14
next a

for a = 0 to c step 0.01
b = ( c ^ 2 - a ^ 2 ) ^ (0.5)
pset (root_x - a, root_y - (b/aspect_ratio)), 13
next a

for a = -c to 0 step 0.01
b = ( c ^ 2 - a ^ 2 ) ^ (0.5)
pset (root_x + a, root_y + (b/aspect_ratio)), 12
next a

for a = 0 to c step 0.01
b = ( c ^ 2 - a ^ 2 ) ^ (0.5)
pset (root_x + a, root_y + (b/aspect_ratio)), 11
next a

   


RE: Playing with code: a circle function using triangle math - bplus - 09-13-2023

this might be interesting:
Code: (Select All)
Screen _NewImage(400, 400, 32)
r = 100
cx = _Width / 2: cy = _Height / 2
For x = 0 To r
    y = Sqr(r * r - x * x)
    PSet (cx - x, cy - y)
    PSet (cx + x, cy - y)
    PSet (cx - x, cy + y)
    PSet (cx + x, cy + y)


    ' remove comments for no holes
    PSet (cx - y, cy - x)
    PSet (cx + y, cy - x)
    PSet (cx - y, cy + x)
    PSet (cx + y, cy + x)
Next



RE: Playing with code: a circle function using triangle math - CharlieJV - 09-14-2023

(09-13-2023, 07:50 PM)bplus Wrote: this might be interesting:
Code: (Select All)
Screen _NewImage(400, 400, 32)
r = 100
cx = _Width / 2: cy = _Height / 2
For x = 0 To r
    y = Sqr(r * r - x * x)
    PSet (cx - x, cy - y)
    PSet (cx + x, cy - y)
    PSet (cx - x, cy + y)
    PSet (cx + x, cy + y)


    ' remove comments for no holes
    PSet (cx - y, cy - x)
    PSet (cx + y, cy - x)
    PSet (cx - y, cy + x)
    PSet (cx + y, cy + x)
Next

That's awesome, and kind of funny.

I've been mulling this in the back of my sponge all day, and I was just about to sit down and code something similar when I saw your reply.

I'm going to write up a blog entry about my own code and a testing app for it to compare performance of the alternative CIRCLE approach compared to the implementation of CIRCLE in wwwBASIC as it was when I nabbed it and how it still is today.


RE: Playing with code: a circle function using triangle math - bplus - 09-14-2023

Check this out 1/2 the drawing and it holds water ie a paint!

Code: (Select All)
Screen _NewImage(400, 400, 32)
r = 100
cx = _Width / 2: cy = _Height / 2
y = r
Color &HFFFFFFFF ' use Alpha to see if we overlap at any point points of overlap will be brighter, for some reason have to use 100% opaque for Paint border
While x < y - 1

    y = Sqr(r * r - x * x)

    PSet (cx - x, cy - y)
    PSet (cx + x, cy - y)
    PSet (cx - x, cy + y)
    PSet (cx + x, cy + y)


    PSet (cx - y, cy - x)
    PSet (cx + y, cy - x)
    PSet (cx - y, cy + x)
    PSet (cx + y, cy + x)
    x = x + 1
    Sleep  'you gotta see this draw
Wend
' does it hold water?
Paint (200, 200), &HFF8888FF, &HFFFFFFFF ' yes!



RE: Playing with code: a circle function using triangle math - CharlieJV - 09-14-2023

I decided to put program links and screenshots in a blog article:

When CIRCLE feels to slow, try and test some triangle math

Good program template to compare various algorithms.


RE: Playing with code: a circle function using triangle math - James D Jarvis - 09-14-2023

You inspired an ellipse filling routine from me:  elcircle (qb64phoenix.com)


RE: Playing with code: a circle function using triangle math - CharlieJV - 09-14-2023

(09-14-2023, 04:12 PM)James D Jarvis Wrote: You inspired an ellipse filling routine from me:  elcircle (qb64phoenix.com)

Holy moly, would you look at that?

Me, not just a once in a blue moon in the dim light pretty face, but occasionally somewhat useful: YES!

That is awesome.  So look forward to trying that later.

Pardon me, I just received a KANDU "KTAK", so I'm totally zenning out over my lunch hour before getting back to the work grind.


BTW, stay tuned.  I'll be taking that circle performance test app and adding two other approaches for the full ménagé à quatre comparison.


RE: Playing with code: a circle function using triangle math - CharlieJV - 09-15-2023

I made substantial changes to that CIRCLE testing program I shared yesterday.  Substantial enough that I had to do a little write-up.

The program now also has two additional circle subroutines for performance comparison: Bresenham's circle algorithm and the midpoint circle algorithm.

Would you believe that my tiny "triangle-math-based" algorithms beats the other two by a hair?

Details in CIRCLE Algorithms Testing Enhanced