Comments (4)
Hey! Happy to help.
So there's not actually a reason to make this a macro -- you can pass functions as arguments to other functions and call them just fine. That'll simplify things a bit.
(defn hypotrochoid [f f2 r big-r t d time]
(+
(* d (f (
(* t (/ (- big-r r) r)
))))
( * (- big-r r) (f2 t)))
)
(toodle
{:speed 5 :position [50 100]}
(set self.position
[
(
hypotrochoid cos sin 3 5 2.3 2 self.age
)
(
hypotrochoid sin cos 3 5 2.3 2 self.age
)
])
)
I renamed fn
and fn2
to f
and f2
, because fn
is a special-form -- you can create a variable called fn
but you won't actually be able to call it -- not sure if that's what led you to a macro in the first place.
That gives the same error as before, but I think it's easier to spot the mistake: you have extra parentheses around the expression (* t (/ (- big-r r) r)
. That expression returns a number, and the extra parentheses make it try to "invoke" the number.
Taking these off gives you something that runs, but probably doesn't do what you want:
(defn hypotrochoid [f f2 r big-r t d time]
(+
(* d (f (* t (/ (- big-r r) r))))
(* (- big-r r) (f2 t))))
(toodle
{:speed 5 :position [50 100]}
(set self.position
[(hypotrochoid cos sin 3 5 2.3 2 self.age)
(hypotrochoid sin cos 3 5 2.3 2 self.age)
]))
It just draws a line. This is because you have two arguments t
and time
, and your expression uses t
but time
is the actual age.
(defn hypotrochoid [f f2 r big-r d t]
(+
(* d (f (* t (/ (- big-r r) r))))
(* (- big-r r) (f2 t))))
(toodle
{:speed 5 :position [50 100]}
(set self.position
[(hypotrochoid cos sin 3 5 2 self.age)
(hypotrochoid sin cos 3 5 2 self.age)
]))
That actually draws something! But it's very small. Let's multiply everything by 10
so we can see it:
(defn hypotrochoid [f f2 r big-r d t]
(+
(* d (f (* t (/ (- big-r r) r))))
(* (- big-r r) (f2 t))))
(toodle
{:speed 5 :position [50 100]}
(set self.position
[(hypotrochoid cos sin 30 50 20 self.age)
(hypotrochoid sin cos 30 50 20 self.age)
]))
Okay, not exactly right yet. The line is because we start at [50 100]
but then immediately move far away from that. There's actually a different way to do drawings when you have a closed-form formula for the position like this: use cloodle
instead:
(defn hypotrochoid [f f2 r big-r d t]
(+
(* d (f (* t (/ (- big-r r) r))))
(* (- big-r r) (f2 t))))
(cloodle
[(hypotrochoid cos sin 30 50 20 age)
(hypotrochoid sin cos 30 50 20 age)])
Instead of setting the position, we just return the new position, and we don't have to specify an initial position -- it will use the first returned value. So that's pretty good.
This draws really fast; let's slow it down a little:
(defn hypotrochoid [f f2 r big-r d t]
(+
(* d (f (* t (/ (- big-r r) r))))
(* (- big-r r) (f2 t))))
(cloodle
(def t (/ age 100))
[(hypotrochoid cos sin 30 50 20 t)
(hypotrochoid sin cos 30 50 20 t)])
And I think that works!
It's not a direct translation of the formula on the wikipedia page, though -- I think a more direct translation is this:
(defn hypotrochoid [f r R t d]
(+
(* d (f (* t (/ (- R r) r))))
(* (- R r) (f t))))
(def d 50)
(cloodle
(def t (/ age 100))
[(hypotrochoid cos 30 50 t d)
(hypotrochoid sin 30 50 t (- d))])
But it seems that swapping the trigonometric function has same effect as negating d
-- I can't 100% convince myself why that works but it seems clever!
from toodle.studio.
I was trying to recall if I was using macros just be fancy, or if there was a reason, and now I remember why. I needed to use a macro, because the formula for X and Y differ by swapping cos/sin functions.
So, I just changed the code to be like this:
(toodle
{:speed 3 :position [100 50]}
(set self.position
[
(hypotrochoid cos sin 3 5 2.3 2 self.age)
(hypotrochoid sin cos 3 5 2.3 2 self.age)
]
))
That still results in the same thing, but I thought the shape of the value I am returning looks better (two numbers, rather than one). But, same error.
from toodle.studio.
Except that defn
isn't a macro, but a function. I need to do some reading. :)
from toodle.studio.
Well, converting to a defmacro
does not look like it works, and I have a lot of unquotes, so I am clearly doing something wrong still.
(defmacro hypotrochoid [fn fn2 r big-r t d time]
~(+
(* ,d (,fn (
(* ,t (/ (- ,big-r ,r) ,r)
))))
( * (- ,big-r ,r) (,fn2 ,t)))
)
(toodle
{:speed 5 :position [50 100]}
(set self.position
[
(
hypotrochoid cos sin 3 5 2.3 2 self.age
)
(
hypotrochoid sin cos 3 5 2.3 2 self.age
)
])
)
Not I get doodle error "1.53333 called with 0 arguments, possibly expected 1"
from toodle.studio.
Related Issues (5)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from toodle.studio.