Cardioid - Heart Curve

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
IcarusLives
Posts: 175
Joined: 17 Jan 2016 23:55

Cardioid - Heart Curve

#1 Post by IcarusLives » 04 Mar 2019 08:09

Just wanted to share something neat I found.

http://mathworld.wolfram.com/HeartCurve.html

I attempted to recreate this in batch. Here is my attempt. The algorithm is correct. This is the 6th example.

I tried to "fine tune" the heart to appear more.. hearty..? A deeper point in the top would be nice, but I cannot seem to manage.

Enjoy :)

Image

Code: Select all

@echo off & setlocal enableDelayedExpansion

for /F %%a in ('echo prompt $E^| cmd') do set "ESC=%%a"
set /a "PI=(35500000/113+5)/10, HALF_PI=(35500000/113/2+5)/10, TWO_PI=2*PI, PI32=PI+PI_div_2, neg_PI=PI * -1, neg_HALF_PI=HALF_PI *-1"
set "_SIN=a-a*a/1920*a/312500+a*a/1920*a/15625*a/15625*a/2560000-a*a/1875*a/15360*a/15625*a/15625*a/16000*a/44800000"
set "sin=(a=(x)%%62832, c=(a>>31|1)*a, a-=(((c-47125)>>31)+1)*((a>>31|1)*62832)  +  (-((c-47125)>>31))*( (((c-15709)>>31)+1)*(-(a>>31|1)*31416+2*a)  ), %_SIN%) / 100"
set "cos=(a=(15708 - x)%%62832, c=(a>>31|1)*a, a-=(((c-47125)>>31)+1)*((a>>31|1)*62832)  +  (-((c-47125)>>31))*( (((c-15709)>>31)+1)*(-(a>>31|1)*31416+2*a)  ), %_SIN%)"

set /a "heartSize=1", "heartHei=13", "heartWid=16", "centerScreen=25"

for /l %%a in (0,250,%TWO_PI%) do (
	set /a "gotPow=1", "normalize=1" & for /l %%b in (1,1,3) do set /a "gotPow*=!sin:x=%%a!", "normalize*=100"
	set /a "x= heartSize * heartWid * gotPow / normalize + centerScreen"
	set /a "y=-heartSize * (heartHei * !cos:x=%%a! - 5 * !cos:x=2*%%a! - 2 * !cos:x=3*%%a! - !cos:x=4*%%a!) / 10000 + centerScreen"
	<nul set /p "=%esc%[!y!;!x!H%esc%[38;5;9mÛ"
)

pause >nul
Last edited by IcarusLives on 27 Oct 2020 22:46, edited 3 times in total.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Cardoid - Heart Curve

#2 Post by Aacini » 04 Mar 2019 08:50

Ops! You are using ANSI sequences to move the cursor... :(

This just works in Windows 10, or with an ANSI driver installed. :cry:

Antonio

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Cardoid - Heart Curve

#3 Post by dbenham » 04 Mar 2019 09:03

I have never heard of a "cardoid". Did you mean "cardioid"?

That shape is not a cardioid because it has two cusps, where as a true cardioid has one cusp. If the bottom was rounded instead of having a point then it could be a cardioid.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Cardoid - Heart Curve

#4 Post by Aacini » 04 Mar 2019 09:14

Interesting!

Image

Code: Select all

@echo off
setlocal EnableDelayedExpansion

for /F %%a in ('echo prompt $E^| cmd') do set "ESC=%%a"
set "_SIN=a-a*a/1920*a/312500+a*a/1920*a/15625*a/15625*a/2560000-a*a/1875*a/15360*a/15625*a/15625*a/16000*a/44800000"
set "SIN=(a=((x) * 31416 / 180)%%62832, c=(a>>31|1)*a, a-=(((c-47125)>>31)+1)*((a>>31|1)*62832)  +  (-((c-47125)>>31))*( (((c-15709)>>31)+1)*(-(a>>31|1)*31416+2*a)  ), %_SIN%) / 100"
set "COS=(a=(15708 - (x) * 31416 / 180)%%62832, c=(a>>31|1)*a, a-=(((c-47125)>>31)+1)*((a>>31|1)*62832)  +  (-((c-47125)>>31))*((((c-15709)>>31)+1)*(-(a>>31|1)*31416+2*a)  ), %_SIN%) / 10000"

set /a "heartSize=1", "heartHei=13", "heartWid=16", "centerScreen=50", "normalize=1000000"

set "line="
for /L %%x in (1,1,70) do set "line=!line! "
for /L %%y in (1,1,70) do set "line[%%y]=%line%"

for /L %%a in (0,1,360) do (
   set "gotPow=1" & for /l %%b in (1,1,3) do set /a "gotPow*=!sin:x=%%a!"
   set /a "x= heartSize * heartWid * gotPow / normalize + centerScreen"
   set /a "y=-heartSize * (heartHei * !cos:x=%%a! - 5 * !cos:x=2*%%a! - 2 * !cos:x=3*%%a! - !cos:x=4*%%a!) + centerScreen"

   set /A "xP1=x+1"
   for /F "tokens=1-3" %%x in ("!x! !y! !xP1!") do set "line[%%y]=!line[%%y]:~0,%%x!@!line[%%y]:~%%z!"
)

cls
color C
for /L %%y in (1,1,70) do echo/!line[%%y]!

pause >nul
Antonio

IcarusLives
Posts: 175
Joined: 17 Jan 2016 23:55

Re: Cardoid - Heart Curve

#5 Post by IcarusLives » 04 Mar 2019 13:26

dbenham wrote:
04 Mar 2019 09:03
I have never heard of a "cardoid". Did you mean "cardioid"?

That shape is not a cardioid because it has two cusps, where as a true cardioid has one cusp. If the bottom was rounded instead of having a point then it could be a cardioid.
Haha, yes that is what I meant. Thank you!

Image

You're also right that a heart and a cardioid are not the same! Sorry for the confusion.

IcarusLives
Posts: 175
Joined: 17 Jan 2016 23:55

Re: Cardoid - Heart Curve

#6 Post by IcarusLives » 04 Mar 2019 13:27

Aacini wrote:
04 Mar 2019 09:14
Interesting!
Thank you for thinking so! And thank you for rewritting a OS friendly version!

misol101
Posts: 475
Joined: 02 May 2016 18:20

Re: Cardioid - Heart Curve

#7 Post by misol101 » 05 Mar 2019 02:39

Thanks again Icarus for inspiration! For cmdgfx users, here is a oneliner:

Code: Select all

cmdgfx_gdi "block 0 100,0,500,70 100,0 -1 0 0 ????=??03 (y+1) & block 0 0,0,500,70 0,0 -1 0 1 - - store(sin(x/3),1)+store((y+16)/16,2)+16*s2*s1*s1*s1+50 store(x/3,1)+store((y+16)/16,2)+13*s2*cos(s1)-5*cos(s1*2)-2*cos(s1*3)-cos(4*s1)+35" f6:0,0,500,70,100,70K
Result:
Image

Replace (y+1) with (y+1+x), and we get:

Image

Now add a red palette to the end of the line for that extra Valentine feeling: 000000,110000,220000,330000,440000,550000,660000,770000,880000,990000,aa0000,bb0000,cc0000,dd0000,ee0000

Image

Ok, that's it. Sorry to invade the thread, hope you like it :oops:

Post Reply