bachus Posted August 12, 2021 Share Posted August 12, 2021 (edited) 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 y where " 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 August 12, 2021 by bachus Quote Link to comment Share on other sites More sharing options...
TGB1718 Posted August 12, 2021 Share Posted August 12, 2021 The 2 formula you have produced will be a single point, you will need a loop incrementing the variables to produce a drawing Quote Link to comment Share on other sites More sharing options...
TGB1718 Posted August 12, 2021 Share Posted August 12, 2021 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 Quote Link to comment Share on other sites More sharing options...
bachus Posted August 12, 2021 Author Share Posted August 12, 2021 (edited) 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. Would you know why this is upside down? Edited August 12, 2021 by bachus Quote Link to comment Share on other sites More sharing options...
Dinadan67 Posted August 12, 2021 Share Posted August 12, 2021 50 PLOT X,100-Y 1 Quote Link to comment Share on other sites More sharing options...
bachus Posted August 12, 2021 Author Share Posted August 12, 2021 (edited) 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!!! Edited August 12, 2021 by bachus 2 Quote Link to comment Share on other sites More sharing options...
stepho Posted August 13, 2021 Share Posted August 13, 2021 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. 1 Quote Link to comment Share on other sites More sharing options...
ivop Posted August 13, 2021 Share Posted August 13, 2021 And SIN(1*TX) is just SIN(TX). Similar with COS(1*TY). That's two multiplications less. BASIC will happily multiply by 1 Quote Link to comment Share on other sites More sharing options...
bachus Posted August 14, 2021 Author Share Posted August 14, 2021 (edited) 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 Edited August 14, 2021 by bachus Quote Link to comment Share on other sites More sharing options...
Roydea6 Posted August 14, 2021 Share Posted August 14, 2021 Line 20 T=2 could be T=1.8 for a better heart resolution that leaves no gaps in the heart. Quote Link to comment Share on other sites More sharing options...
billkendrick Posted August 16, 2021 Share Posted August 16, 2021 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 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.