Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 19:02 15 May 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Micromite Spirograph

Author Message
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8601
Posted: 11:07pm 14 Mar 2019
Copy link to clipboard 
Print this post

Try running the following extremely simple code




example data on SSD1963: 200,170,80


Option explicit
option default NONE
dim float x,y,t
dim float a, b, p
input "params a,b,p :",a,b,p
cls
do
x=(a-b)*cos(rad(t))+(b-p)*cos((a-b)/b*rad(t))+mm.hres\2
y=(a-b)*SIN(rad(t))-(b-p)*SIN((a-b)/b*rad(t))+mm.vres\2
pixel x,y
t=t+1
loop while not loc(0)


 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 5923
Posted: 02:00am 15 Mar 2019
Copy link to clipboard 
Print this post

And there I was thinking you had come up with a spirometer to go with the ECG from a few weeks ago

Jim
VK7JH
MMedit   MMBasic Help
 
paceman
Guru

Joined: 07/10/2011
Location: Australia
Posts: 1329
Posted: 05:06am 15 Mar 2019
Copy link to clipboard 
Print this post

That was my first thought too Jim till I looked at how short the code was. I ran it with my MM+ on a 4" LCD and was surprised at just how fast it all happened. Think I'll have to have a fiddle with it - different colours and stuff too.

Nice job Peter!

Greg
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8601
Posted: 09:12am 15 Mar 2019
Copy link to clipboard 
Print this post

Here is a modified version which uses a recursive calculation of the greatest common denominator of a and b to predict how many petals the design will have and automatically terminates the program when the complete pattern is drawn.

I've put a short pause in the loop so you can see the pattern drawing





Option explicit
Option default NONE
Dim float x,y,x0,y0, t
Dim float a, b, h
dim integer d
Input "params a,b,h :",a,b,h
d=gcd(a,b)
Print "Pattern will have ",a\d," petals"
CLS
x0=(a-b)*Cos(Rad(0))+(h)*Cos((a-b)/b*Rad(0))+MM.HRes\2
y0=(a-b)*Sin(Rad(0))-(h)*Sin((a-b)/b*Rad(0))+MM.VRes\2
For t=1 To (a\d-1)*360
x=(a-b)*Cos(Rad(t))+(h)*Cos((a-b)/b*Rad(t))+MM.HRes\2
y=(a-b)*Sin(Rad(t))-(h)*Sin((a-b)/b*Rad(t))+MM.VRes\2
LINE x0,y0, x,y
x0=x
y0=y
Pause 1
Next t

Function gcd(a As integer, b As integer)As integer
' returns greatest common denominator of two numbers
If(b=0)Then
gcd=a
else
gcd=gcd(b, a mod b)
EndIf
End Function


200,105,90




Edited by matherp 2019-03-16
 
paceman
Guru

Joined: 07/10/2011
Location: Australia
Posts: 1329
Posted: 12:47pm 15 Mar 2019
Copy link to clipboard 
Print this post

Your diagram reminds me of plotting the locus of a point with interlocking circles when I was at college. It was 'orrible as I remember, no PC's back then, but we were using the generic circle equations I think, not trig functions.

I played colours with your first code - try the one below. I fixed a & b and then there's a parameter 'inc' to auto change 'p' between colours. Using inc=2 makes me feel a bit sick as it progresses, (flowerpower-ish); inc=4 is quite pretty.

Your auto finish code would have been handy here - I just fiddled with 't' till it seemed pretty close.

Greg

'MatherP's Spirograph - https://www.thebackshed.com/forum/forum_posts.asp?TID=11232&PN=1
'try 200,170,80 as the parameters
'also try inc=2 & inc=4. 2 is LSD-ish and 4 is pretty!

Option explicit
option default NONE

const blue=rgb(blue),green=rgb(green),yellow=rgb(yellow),red=rgb(red),black=rgb(black)
const cyan=rgb(cyan),magenta=rgb(magenta),BRGRAY=rgb(100,100,100),LTGRAY=rgb(60,60,60)

dim float x,y,t
dim float a, b, p
dim integer culla(5)=(blue,green,yellow,red,cyan,magenta),z,inc
'input "params a,b,p :",a,b,p
a=200: b=170: p=80
input "parameter inc = ",inc
cls
change: 'cls
do
x=(a-b)*cos(rad(t))+(b-p)*cos((a-b)/b*rad(t))+mm.hres\2
y=(a-b)*SIN(rad(t))-(b-p)*SIN((a-b)/b*rad(t))+mm.vres\2
pixel x,y,culla(z)
t=t+1
loop while t<6200 'not loc(0)
z=z+1: t=0: p=p+inc
goto change
'---------------------------------
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3027
Posted: 03:10pm 15 Mar 2019
Copy link to clipboard 
Print this post

  paceman said  ... I played colours with your first code

Nice on a 4.3" SSD1963 on an H7 backpack-144:


But I got this:

save "spirograph2.bas"
> run
parameter inc = 4
[22] Pixel x,y,culla(z)
Error: Index out of bounds
>

Don't know what more should have appeared.

PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
paceman
Guru

Joined: 07/10/2011
Location: Australia
Posts: 1329
Posted: 02:10am 16 Mar 2019
Copy link to clipboard 
Print this post

Nothing more Lance, that was it. I was lazy finishing it off. I'm using a 4.3" SSD1963 too - did using inc=2 make your eyes go a bit weird too?

Greg
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3677
Posted: 07:48am 16 Mar 2019
Copy link to clipboard 
Print this post

  lizby said   But I got this:

save "spirograph2.bas"
> run
parameter inc = 4
[22] Pixel x,y,culla(z)
Error: Index out of bounds
>


z should maybe wrap around instead of going on incrementing, or the program should stop when it reaches the size of culla()

JohnEdited by JohnS 2019-03-17
 
paceman
Guru

Joined: 07/10/2011
Location: Australia
Posts: 1329
Posted: 11:55am 16 Mar 2019
Copy link to clipboard 
Print this post

OK, a quick wrap-around. Add the line below as the second last line, i.e before the 'GOTO change'.

If z=6 then z=0

Stop it with a ctrlZ when you think it's pretty enough.

Greg

P.S. I'm almost a bit embarassed posting this after Peter's efforts.
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3027
Posted: 03:16pm 16 Mar 2019
Copy link to clipboard 
Print this post

Interesting indeed, but it would take a video to do it justice with either inc=4 or inc=2.



Ctrl-C terminates.

PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
PeterB
Guru

Joined: 05/02/2015
Location: Australia
Posts: 641
Posted: 01:35am 17 Mar 2019
Copy link to clipboard 
Print this post

G'Day.

All the bloke asked for was a polygon! Have you no self control ?
But it is very impressive.

Peter
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024