Jump to content
IGNORED

Atari BASIC / TBXL help with drawing heart shape


Recommended Posts

Hi,

I'm looking for some help with applying math equation to draw a heart shape in Atari BASIC.

The equation is for heart #6, which comes from https://mathworld.wolfram.com/HeartCurve.html.

 

"The sixth heart curve can be defined parametrically as:

x = 16sin^3t
 
y = 13cost-5cos(2t)-2cos(3t)-cos(4t).
 

where t in [-1,1]"

 

Currently, I have this code but it does not work. It only draws a single dot. I'm sure I have the "t" definition wrong in line 30 and I'm not too confident if I got the formula right in line 40... 

 

10 REM DRAW A HEART
20 GRAPHICS 7:SETCOLOR 2,4,5:COLOR 1
30 TX=-1: TY=1
40 X=16*(3^SIN(1*TX)): Y=13*COS(1*TY)-5*COS(2*TY)-2*COS(3*TY)-COS(4*TY)
40 PLOT X,Y

Would there be a kind soul around here who could help me fix this code? Thank you in advance.

Edited by bachus
Link to comment
Share on other sites

Have a look at this, it doesn't produce a heart, but vaguely like an upside down one :)

 

see the loop increments of TX and TY, also the results can be negative, so you have to add an offset

to get it on screen.

 

Maybe the shape is wrong because the formula's need a bit of tweaking

 

10 REM DRAW A HEART
20 GRAPHICS 7:SETCOLOR 2,4,5:COLOR 1
25 TX=-1:FOR TY=1 TO 180
40 X=16*(3^SIN(1*TX)):Y=13*COS(1*TY)-5
*COS(2*TY)-2*COS(3*TY)-COS(4*TY)
45 Y=Y+50:X=X+50: REM MAKE X AND Y POSITIVE TO FIT SCREEN
50 PLOT X,Y
55 TX=TX-1
60 NEXT TY
70 GOTO 70

Link to comment
Share on other sites

Thank you, TGB1718. This helped a lot.

 

I found on the same website (https://mathworld.wolfram.com/TrigonometricPowerFormulas.html) that formula "16*sin^3(t)" used to calculate X, is the same as "16*(0.25*(3sin(t) - sin(3t)))"

 

So, I figured that I had the sin^3(x) implemented wrong in BASIC. I'm not exactly sure how to fix this, so I swapped for the other formula:

 

10 REM DRAW A HEART
20 GRAPHICS 7:SETCOLOR 2,4,5:COLOR 1
25 TX=-1:FOR TY=1 TO 180
40 X=16*((0.25*(3*SIN(1*TX)-SIN(3*TX)))):Y=13*COS(1*TY)-5*COS(2*TY)-2*COS(3*TY)-COS(4*TY)
45 Y=Y+50:X=X+50: REM MAKE X AND Y POSITIVE TO FIT SCREEN
50 PLOT X,Y
55 TX=TX-1
60 NEXT TY
70 GOTO 70

 

... and got this ? Perfect shape, but upside down.

image.thumb.png.0c97c32bef753de8566461c05e54c791.png

 

Would you know why this is upside down?

Edited by bachus
Link to comment
Share on other sites

Thank you, Dinadan67. That did it!

 

Again, thank you both for help. Very appreciated!

 

I made a couple more tweaks to: double the count of dots (Line 25), double the size of the heart (defined T variable in line 30 and added it to X and Y formulas in Line 40) and make it fit after above changes (Line 45).

 

10 REM DRAW A HEART
20 GRAPHICS 7:SETCOLOR 2,4,5:COLOR 1
25 TX=-1:FOR TY=1 TO 360
30 T=2: REM DOUBLE THE HEART SIZE
40 X=T*(16*((0.25*(3*SIN(1*TX)-SIN(3*TX))))):Y=T*(13*COS(1*TY)-5*COS(2*TY)-2*COS(3*TY)-COS(4*TY))
45 Y=Y+75:X=X+80: REM MAKE X AND Y POSITIVE TO FIT SCREEN
50 PLOT X,100-Y
55 TX=TX-1
60 NEXT TY

 

... and now got this ? Perfect!!!

 

image.thumb.png.ea855882676b4b002d12435f5f384f06.png

Edited by bachus
  • Like 2
Link to comment
Share on other sites

7 hours ago, bachus said:

45 Y=Y+75:X=X+80: REM MAKE X AND Y POSITIVE TO FIT SCREEN
50 PLOT X,100-Y
 

Try:

45 Y=25-Y:X=X+80: REM MAKE X AND Y POSITIVE TO FIT SCREEN
50 PLOT X,Y

Should give the same results for one less calculation.

  • Like 1
Link to comment
Share on other sites

22 hours ago, stepho said:

Try:

45 Y=25-Y:X=X+80: REM MAKE X AND Y POSITIVE TO FIT SCREEN
50 PLOT X,Y

Should give the same results for one less calculation.

Thank you for the suggestion. Added. I actually rolled it further into Line 40.

12 hours ago, ivop said:

And SIN(1*TX) is just SIN(TX). Similar with COS(1*TY). That's two multiplications less. BASIC will happily multiply by 1 :)

Hehe, right. That makes perfect sense. Thanks.

 

In the end I added a timer to check how much time those tweaks saved. All the help was definitely worth it. Although, the things is slow, that's for sure. One day, assembly language here I come... Hehe.

 

Thank you ALL very much for help!

 

10 REM DRAW A HEART
15 GRAPHICS 7:SETCOLOR 2,4,5:COLOR 1
20 T=2: REM DOUBLE THE HEART SIZE
25 STARTTIME = INT((PEEK(18) * 65536 + PEEK(19) * 256 + PEEK(20))): REM START THE CLOCK
30 TX=-1:FOR TY=1 TO 360
40 X=(T*(16*((0.25*(3*SIN(TX)-SIN(3*TX))))))+80:Y=25-(T*(13*COS(TY)-5*COS(2*TY)-2*COS(3*TY)-COS(4*TY)))
50 PLOT X,Y
55 TX=TX-1
60 NEXT TY
65 ENDTIME = INT((PEEK(18) * 65536 + PEEK(19) * 256 + PEEK(20))): REM STOP THE CLOCK
70 PRINT "ELAPSED TIME: ";(ENDTIME-STARTTIME)/50;" SECONDS.":PRINT "": REM SHOW TIME ELAPSED
75 GOTO 75

 

image.thumb.png.ae90de3c015583bde3be311a91ce179d.png

Edited by bachus
Link to comment
Share on other sites

On 8/13/2021 at 7:56 PM, bachus said:

Thank you for the suggestion. Added. I actually rolled it further into Line 40.

Hehe, right. That makes perfect sense. Thanks.

 

In the end I added a timer to check how much time those tweaks saved. All the help was definitely worth it. Although, the things is slow, that's for sure. One day, assembly language here I come... Hehe.

 

Thank you ALL very much for help!

 

I fed it to the Atari 8-bit Bot over on Twitter :)

 

 

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...