# Computer aided Designs

A design, (written in another langauge),
The code is not to long, but it would be good to put on the code shareing section. Its from a Youtube video, needs translation it to LUA…
The video : Learn programming 67: Circular motion: sine and her cousin_.
I’m not to sure if this is going to be to much work, but I would like the fourm to take a little peek.

``````function setup ( )
float a = 0
float b = 0
float co = 0 -- co stands for color
function draw ( )
background (40, 40, 50)
fill (255, 255, 255, 255)
strokeWidth(1)
float x0 = map(sin(a), -1, 1 2-, width -20) -- math.sin ?
float y0 = map(cos(a), -1, 1 2-, height -20)
float x1 = map(sin(b), -1, 1 2-, width -20)
float y1 = map(cos(b), -1, 1 2-, height-20)
line(x0, y0, x1, y1)
line(x0, y0, x1, y1)
co = co + 1
if (co > 100) {
co = 0
}
end
``````

@kendog400 To post code correctly, put ~~~ on a line before and after the code. I added them to your code above. Maybe instead of posting code that someone has to convert to Codea, you can convert it yourself. That would be a great way for you to learn how to use Codea. If you run into trouble, just ask and you’ll get a lot of help from us. That’s how a lot of members learned how to program Codea. They wrote small programs and then increased the size as they learned more.

@kendog400 Here a Codea version. Change the slider values to change the design.

``````supportedOrientations(LANDSCAPE_ANY)

function setup()
parameter.integer("a1",1,30,3,reset)
parameter.integer("b1",1,30,5,reset)
a,b=0,0
backingMode(RETAINED)
end

function draw()
stroke(0, 213, 255, 255)
strokeWidth(1)
x0=math.sin(a)*WIDTH/2+WIDTH/2
y0=math.cos(a)*HEIGHT/2+HEIGHT/2
x1=math.sin(b)*WIDTH/2+WIDTH/2
y1=math.cos(b)*HEIGHT/2+HEIGHT/2
line(x0,y0,x1,y1)
line(x0,y0,x1,y1)
a=a+a1/100
b=b+b1/100
if a1==b1 then
fill(255)
text("a1 and b1 shouldn't be equal",WIDTH/2,HEIGHT/2)
end
end

function reset()
a,b=0,0
backingMode(RETAINED)
end
``````

Here’s a version with 3 lines and colors.

``````supportedOrientations(LANDSCAPE_ANY)

function setup()
parameter.integer("a1",1,30,3,reset)
parameter.integer("b1",1,30,5,reset)
parameter.integer("c1",1,30,7,reset)
a,b,c=0,0,0
backingMode(RETAINED)
end

function draw()
stroke(0, 213, 255, 255)
strokeWidth(1)
x0=math.sin(a)*WIDTH/2+WIDTH/2
y0=math.cos(a)*HEIGHT/2+HEIGHT/2
x1=math.sin(b)*WIDTH/2+WIDTH/2
y1=math.cos(b)*HEIGHT/2+HEIGHT/2
x2=math.sin(c)*WIDTH/2+WIDTH/2
y2=math.cos(c)*HEIGHT/2+HEIGHT/2
stroke(255,0,0)
line(x0,y0,x1,y1)
stroke(0,255,0)
line(x0,y0,x2,y2)
stroke(0,0,255)
line(x1,y1,x2,y2)
a=a+a1/100
b=b+b1/100
c=c+c1/100
end

function reset()
a,b,c=0,0,0
backingMode(RETAINED)
end
``````

Thank you very much…

Opp’s I forgot to put math. in front of sin & cos…
I got this confused with java…
I would like to cross platform java to lua…
There are a lot of gud tutorials online written in java,
nice to change them to lua…
I still get only one ellipse…

No more Error msg : But still only get one ellipse
in center of screen

– Coding challange #30 phylotaxis

``````displayMode(FULLSCREEN)

function setup ()
n = 0
c = 2
end

function draw ()

background (0)

a = n * 137.5
r = c * math.sqrt (n)
x = r * math.cos (a) + WIDTH / 2
y = r * math.sin (a) + HEIGHT / 2
fill (0, 0, 255)
ellipse (x, y, 8, 8)
end
``````

@kendog400 Anytime you use math functions, they have to be math.sqrt, math.cos, math.sin. See the built in reference for more info. Here’s an example.

``````displayMode(FULLSCREEN)

function setup()
angle=0
fill(255)
end

function draw()
background(0)
ellipse(WIDTH/2,HEIGHT/2,5)
ellipse(x+WIDTH/2,y+HEIGHT/2,20)
angle=angle+.02
end
``````

I have here another interesting computer aided design,
(written in another langauge).
The code is not to long.
Its from a Youtube video, and needs translation it to Lua…
The video : Fun programming 79 : A spinning star becomes a plant…
I’m not to sure if this is going to be to much work, but I would like the fourm to take a little peek.
What I would here is to do is study the video, and learn how to translate to Lua, cross reference, and study…

Here’s a link to the site just in case someone else wants to see it. It’s interesting because it has different programs that can be run. It would take some work to convert the code to Codea in some of the examples, but it might be an interesting way to learn.

``````https://www.funprogramming.org/79-A-spinning-star-becomes-a-plant.html
``````

@kendog400 Here’s something similar. It takes about 5-10 seconds to create the image. If the programs crashes, reduce the number 40 in the statement `for z=1,40 do` in setup().

``````displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT_ANY)

function setup()
assert(craft, "Please include Craft as a dependency")
assert(OrbitViewer, "Please include Cameras (not Camera) as a dependency")
scene = craft.scene()
scene.physics.gravity=vec3(0,0,0)
skyMaterial=scene.sky.material
skyMaterial.sky=color(181, 160, 97, 255)
skyMaterial.horizon=color(168, 149, 106, 255)
scene.sun.rotation=quat.eulerAngles(20,45,-30)
tab={}
for z=1,40 do
bezier()
end
for z=1,#tab do
createSphere(tab[z].x,tab[z].y,tab[z].z)
end
end

function draw()
update(DeltaTime)
scene:draw()
fill(255)
text("Slide your finger around the screen to rotate the image",WIDTH/2,HEIGHT-30)
end

function update(dt)
scene:update(dt)
end

function createSphere(x,y,z)
sphere1=scene:entity()
sphere1.position=vec3(x,y,z)
sphere1.model = craft.model.icosphere(1.5,2)
sphere1.material = craft.material("Materials:Specular")
r=math.random(255)
g=math.random(255)
b=math.random(255)
sphere1.material.diffuse=color(r,g,b)
end

function bezier()
x1=math.random(-100,100)
y1=math.random(-60,-10)
z1=math.random(-100,100)
x={0,0,x1*(math.random(100)/20),x1}
y={-150,120,120,y1}
z={0,0,z1*(math.random(100)/20),z1}
for t = 0,1,.01 do
xt = (1-t)^3 * x[1] + 3 * t * (1-t)^2 * x[2] +
3 * t^2 * (1-t) * x[3] + t^3 * x[4]
yt = (1-t)^3 * y[1] + 3 * t * (1-t)^2 * y[2] +
3 * t^2 * (1-t) * y[3] + t^3 * y[4]
zt = (1-t)^3 * z[1] + 3 * t * (1-t)^2 * z[2] +
3 * t^2 * (1-t) * z[3] + t^3 * z[4]
table.insert(tab,vec3(xt,yt,zt))
end
end
``````

I had nothing better to do so here’s a program that draws 20 3D BÃ©zier curves between 2 red balls. It takes 5 to 10 seconds to create the image. For another image, press the restart icon and wait another 5 to 10 seconds.

``````displayMode(FULLSCREEN)

function setup()
assert(craft, "Please include Craft as a dependency")
assert(OrbitViewer, "Please include Cameras (not Camera) as a dependency")
scene = craft.scene()
skyMaterial=scene.sky.material
skyMaterial.sky=color(158, 202, 223, 255)
skyMaterial.horizon=color(98, 166, 114, 255)
scene.sun.rotation=quat.eulerAngles(20,45,-30)
v.ry=90
val=300
for z=1,20 do
x1=math.random(-val,val)
y1=math.random(-val,val)
z1=math.random(-val,val)
x2=math.random(-val,val)
y2=math.random(-val,val)
z2=math.random(-val,val)
bezier()
end
r,g,b=255,0,0
createSphere(0,0,-150,15)
createSphere(0,0,150,15)
end

function draw()
update(DeltaTime)
scene:draw()
fill(255)
text("Slide your finger around the screen to rotate the image.",WIDTH/2,HEIGHT-25)
text("Use two fingers to zoom in, out or to move the image.",WIDTH/2,HEIGHT-50)
end

function update(dt)
scene:update(dt)
end

function bezier()
tab1={}
x={0,0}
y={0,0}
z={-150,150}
for t = 0,1,.01 do
xt = (1-t)^3 * x[1] + 3 * t * (1-t)^2 * x1 + 3 * t^2 * (1-t) * x2 + t^3 * x[2]
yt = (1-t)^3 * y[1] + 3 * t * (1-t)^2 * y1 + 3 * t^2 * (1-t) * y2 + t^3 * y[2]
zt = (1-t)^3 * z[1] + 3 * t * (1-t)^2 * z1 + 3 * t^2 * (1-t) * z2 + t^3 * z[2]
table.insert(tab1,vec3(xt,yt,zt))
end
r=math.random(255)
g=math.random(255)
b=math.random(255)
for z=1,#tab1 do
createSphere(tab1[z].x,tab1[z].y,tab1[z].z,2)
end
end

function createSphere(x,y,z,size)
pt=scene:entity()
pt.position=vec3(x,y,z)
pt.model = craft.model.icosphere(size,2)
pt.material = craft.material("Materials:Specular")
pt.material.diffuse=color(r,g,b)
end
``````

Just sitting on the couch watching the Olympics. Thought I’d throw this together. It takes a few seconds to load.

``````displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
assert(craft, "Please include Craft as a dependency")
assert(OrbitViewer, "Please include Cameras (not Camera) as a dependency")
scene = craft.scene()
skyMaterial=scene.sky.material
skyMaterial.sky=color(158, 202, 223, 255)
skyMaterial.horizon=color(105, 188, 227, 255)
scene.sun.rotation=quat.eulerAngles(90,0,0)
r,g,b=255,0,0
circ(100,15)
r,g,b=0,255,0
circ(50,-15)
r,g,b=0,0,0
circ(0,15)
r,g,b=255,255,0
circ(-50,-15)
r,g,b=0,0,255
circ(-100,15)
end

function draw()
update(DeltaTime)
scene:draw()
v.ry=v.ry+.3
end

function update(dt)
scene:update(dt)
end

function circ(xp,yp)
for a=0,360,2 do
size=8
if xp==50 or xp==-50 then
size=7
end
createSphere(x+xp,y+yp,0,size)
end
end

function createSphere(x,y,z,size)
pt=scene:entity()
pt.position=vec3(x,y,z)
pt.model = craft.model.icosphere(size,2)
pt.material = craft.material("Materials:Specular")
pt.material.diffuse=color(r,g,b)
end
``````

I seem to be getting error msg,s…Any idea what went wrong ? I have a screenshot here of the error msg…

This cud be a slower i-Pad…

I tried all 3 of the pgm’s, all 3 gave error msg’s…

@kendog400 Just as the error message says, you need to include Craft as a dependency. The next error message you would get is to include Cameras as a dependency. Because these programs use Craft code, you need to check Craft and Cameras from the dependency list for the programs to run.

I think I’m lost, how do you include cameras & craft as a dependency ?
This is the 1st time I ever had to do this…

I was able to find craft, and I checked the box, but I never see cameras as a dependency…

I think I got it now…thanks !

Here’s a variation on my 3D BÃ©zier code above.

``````displayMode(FULLSCREEN)

function setup()
pos=0
tab1={}
assert(craft, "Please include Craft as a dependency")
assert(OrbitViewer, "Please include Cameras (not Camera) as a dependency")
scene = craft.scene()
skyMaterial=scene.sky.material
skyMaterial.sky=color(158, 202, 223, 255)
skyMaterial.horizon=color(98, 166, 114, 255)
scene.sun.rotation=quat.eulerAngles(20,45,-30)
v.ry=90
val=300
for z=1,20 do
x1=math.random(-val,val)
y1=math.random(-val,val)
z1=math.random(-val,val)
x2=math.random(-val,val)
y2=math.random(-val,val)
z2=math.random(-val,val)
bezier()
end
r,g,b=255,0,0
createSphere(0,0,-150,15)
createSphere(0,0,150,15)
r,g,b=255,255,255
createSphere(tab1[1].x,tab1[1].y,tab1[1].z,10)
end

function draw()
update(DeltaTime)
scene:draw()
fill(255)
text("Slide your finger around the screen to rotate the image.",WIDTH/2,HEIGHT-25)
text("Use two fingers to zoom in, out or to move the image.",WIDTH/2,HEIGHT-50)
if pos<#tab1 then
pos=pos+1
else
pos=1
end
mv.position=vec3(tab1[pos].x,tab1[pos].y,tab1[pos].z)
end

function update(dt)
scene:update(dt)
end

function bezier()
local tab1={}
x={0,0}
y={0,0}
z={-150,150}
dir=not dir
a,b,c=0,1,.01
if dir then
a,b,c=1,0,-.01
end
for t=a,b,c do
xt = (1-t)^3 * x[1] + 3 * t * (1-t)^2 * x1 + 3 * t^2 * (1-t) * x2 + t^3 * x[2]
yt = (1-t)^3 * y[1] + 3 * t * (1-t)^2 * y1 + 3 * t^2 * (1-t) * y2 + t^3 * y[2]
zt = (1-t)^3 * z[1] + 3 * t * (1-t)^2 * z1 + 3 * t^2 * (1-t) * z2 + t^3 * z[2]
table.insert(tab1,vec3(xt,yt,zt))
end
r=math.random(255)
g=math.random(255)
b=math.random(255)
for z=1,#tab1 do
createSphere(tab1[z].x,tab1[z].y,tab1[z].z,2)
end
end

function createSphere(x,y,z,size)
local pt=scene:entity()