Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Computer aided Designs

edited July 2017 in General Posts: 117

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
11.jpg 45.1K

Comments

  • dave1707dave1707 Mod
    Posts: 7,195

    @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.

  • dave1707dave1707 Mod
    Posts: 7,195

    @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
    
  • dave1707dave1707 Mod
    Posts: 7,195

    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
    
  • Posts: 117

    Thank you very much.....

  • Posts: 117

    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.....

    Pic.jpg 97.9K
  • dave1707dave1707 Mod
    Posts: 7,195

    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
    
  • dave1707dave1707 Mod
    Posts: 7,195

    @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)
        v=scene.camera:add(OrbitViewer, vec3(0,0,0), 400, 0, 400)
        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()
        s1=sphere1:add(craft.rigidbody,STATIC)
        sphere1.position=vec3(x,y,z)
        sphere1.model = craft.model.icosphere(1.5,2)
        sphere1:add(craft.shape.sphere,1.5)
        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
    
  • dave1707dave1707 Mod
    Posts: 7,195

    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=scene.camera:add(OrbitViewer, vec3(0,0,0), 400, 0, 700)
        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()
        pt1=pt:add(craft.rigidbody,STATIC)
        pt.position=vec3(x,y,z)
        pt.model = craft.model.icosphere(size,2)
        pt:add(craft.shape.sphere,size)
        pt.material = craft.material("Materials:Specular")
        pt.material.diffuse=color(r,g,b)
    end
    
  • dave1707dave1707 Mod
    Posts: 7,195

    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)
        v=scene.camera:add(OrbitViewer, vec3(0,0,0), 350, 0, 700)
        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
            x=math.sin(math.rad(a))*40
            y=math.cos(math.rad(a))*40
            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()
        pt1=pt:add(craft.rigidbody,STATIC)
        pt.position=vec3(x,y,z)
        pt.model = craft.model.icosphere(size,2)
        pt:add(craft.shape.sphere,size)
        pt.material = craft.material("Materials:Specular")
        pt.material.diffuse=color(r,g,b)
    end
    
  • Posts: 117

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

  • Posts: 117

    This cud be a slower i-Pad...

  • Posts: 117

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

  • dave1707dave1707 Mod
    Posts: 7,195

    @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.

  • Posts: 117

    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...

  • Posts: 117

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

  • Posts: 117

    I think I got it now...thanks !

  • dave1707dave1707 Mod
    Posts: 7,195

    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=scene.camera:add(OrbitViewer, vec3(0,0,0), 450, 0, 1000)
        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()
        pt1=pt:add(craft.rigidbody,STATIC)
        pt.position=vec3(x,y,z)
        pt.model = craft.model.icosphere(size,2)
        pt:add(craft.shape.sphere,size)
        pt.material = craft.material("Materials:Specular")
        pt.material.diffuse=color(r,g,b)
        if size==2 then
            table.insert(tab1,vec3(x,y,z))
        elseif size==10 then
            mv=pt
        end
    end
    
  • Posts: 338

    @dave1707 nice! if you could implement true 3d tubes that would be great- it is something i’ve wanted for a long time!

  • dave1707dave1707 Mod
    Posts: 7,195

    @piinthesky You can create 3D tubes. Here an example of a square tube. You could probably create a circular tube, but I didn’t want to spend that much time trying to do it.

    displayMode(FULLSCREEN)
    
    function setup()
        assert(craft, "Please include Craft as a dependency")
        assert(OrbitViewer, "Please include Cameras 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(45,45,-80)
        v=scene.camera:add(OrbitViewer, vec3(0,0,0), 20, 0, 700)
        v.ry=15
        v.rx=10
        createTube()
    end
    
    function draw()
        update(DeltaTime)
        scene:draw() 
    end
    
    function update(dt)
        scene:update(dt)
    end
    
    function createTube()
        pt=scene:entity()
        pt.model = craft.model.cube()
        pt.model.positions=
        {   vec3(-3,-3,40),vec3(-3,3,40),vec3(-3,3,0),vec3(-3,-3,0),
            vec3(3,-3,40),vec3(3,3,40),vec3(3,3,0),vec3(3,-3,0),
            vec3(-3,-3,40),vec3(-3,3,40),vec3(-3,3,0),vec3(-3,-3,0),
            vec3(3,-3,40),vec3(3,3,40),vec3(3,3,0),vec3(3,-3,0),
        }    
        pt.model.indices=
        {   1,3,4, 1,2,3, 8,7,5, 7,6,5,  
            12,11,9, 11,10,9, 13,15,16, 13,14,15,
            3,2,6, 3,6,7, 5,1,4, 8,5,4,
            14,10,11, 15,14,11, 12,9,13, 12,13,16,
        }
        pt.material = craft.material("Materials:Specular")    
        pt.material.map = readImage("Blocks:Gravel Stone")
    end
    
  • dave1707dave1707 Mod
    edited February 20 Posts: 7,195

    @piinthesky I did a little more work on the 3D tube. Here’s a better example. You can change the number of sides to alter the roundness of the tube. You can also change the length len of the tube and the diameter dia.

    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(105, 188, 227, 255)
        scene.sun.rotation=quat.eulerAngles(45,90,80)
        v=scene.camera:add(OrbitViewer, vec3(0,0,0), 250, 0, 700)
        v.rx=10
        v.ry=-20
        len=200
        dia=10
        sides=20
        pos={}
        -- calculate vertex values
        for z=0,359,360/sides do
            x=math.sin(math.rad(z))*dia
            y=math.cos(math.rad(z))*dia
            table.insert(pos,vec3(x,y,0))
            table.insert(pos,vec3(x,y,len))
        end
        ind={}
        v=0
        ss={1,2,4,1,4,3,4,2,1,3,4,1}
        -- calculate indices values
        for z=1,#pos/2 do
            for w=1,#ss do
                val=ss[w]+v
                if val>#pos then
                    val=val-#pos
                end
                table.insert(ind,val)
            end
            v=v+2
        end 
        col={}
        for z=1,#pos do
            table.insert(col,color(math.random(255),math.random(255),math.random(255)))
        end
        createTube()
    end
    
    function draw()
        update(DeltaTime)
        scene:draw() 
        fill(255)
        text("Slide your finger around the screen to rotate the object.",WIDTH/2,HEIGHT-30)
        text("Use two fingers to zoom in, out, or move the object.",WIDTH/2,HEIGHT-60)
    end
    
    function update(dt)
        scene:update(dt)
    end
    
    function createTube()
        pt=scene:entity()
        pt.model = craft.model.cube()
        pt.model.positions=pos
        pt.model.indices=ind
        pt.model.colors=col
        pt.material = craft.material("Materials:Basic")    
    end
    
  • Posts: 338

    thanks @dave1707 - very nice! Could be interesting to combined it with your bezier curves program to have 3d tubes that follow the path of the bezier (or a list of predefined 3d coordinates).

  • dave1707dave1707 Mod
    edited February 20 Posts: 7,195

    @piinthesky I don’t know how much trouble there would be connecting the tubes. Maybe I’ll have to try.

  • Posts: 422

    I have quite a bit of code for constructing tubes along paths. You can see an example at

    Most of the necessary code is actually in the Roller Coaster project as that works out all the directions (normal and binormal) along the path which are needed to make the segments fit together. Once that's in place, making the tube fit is quite straightforward.

  • dave1707dave1707 Mod
    Posts: 7,195

    @LoopSpace Nice Example. I looked thru the Roller Coaster Example, but there’s a lot of code to go thru. Do you have anything that works with Craft.

  • edited February 25 Posts: 422

    I've extracted what I could. It needs the PseudoMesh and MeshExt code from my Graphics Libraray. The code would be slightly simpler if using the VecExt code from my Mathematics Library but I've copied the relevant stuff in.

    function setup()
        assert(craft, "Please include Craft as a dependency")
        assert(OrbitViewer, "Please include Cameras (not Camera) as a dependency")
    
        scene = craft.scene()
        scene.camera:add(OrbitViewer)
        local track = scene:entity()
        local p = f(0)
        local np,nt,nq,ax,ang
    
        local t = (f(.001) - f(-.001))/0.002
        local it = t
        local nml = vec3(1,0,0)
        local pts = {{p,it,nml}}
        for k=1,100 do
            np = f(k/100*2*math.pi)
            nt = (f(k/100*2*math.pi+.001) - f(k/100*2*math.pi-.001))/0.002
            ax = t:cross(nt)
            ang = math.asin(ax:len()/(t:len()*nt:len()))
            nml = nml:rotate(ang,ax)
    
            table.insert(pts,{np,nt,nml})
            t = nt
            p = np
        end
        local __track = PseudoMesh()
        local ends = 1
        for k=1,100 do
            if k == 100 then
                ends = 2
            end
            __track:addCylinder({
                startCentre = pts[k][1],
                startWidth = pts[k][3],
                startHeight = pts[k][2]:cross(pts[k][3]):normalize(),
                endCentre = pts[k+1][1],
                endWidth = pts[k+1][3],
                endHeight = pts[k+1][2]:cross(pts[k+1][3]):normalize(),
                ends = ends,
                faceted = false,
                size=50
            })
            ends = 0
        end
        track.model = __track:toModel()
        track.material = craft.material("Materials:Standard")
    end
    
    function draw()
        background(40,40,50)
        update(DeltaTime)
        scene:draw()
    end
    
    function update(dt)
        scene:update(dt)
    end
    
    local mt = getmetatable(vec3())
    mt.rotate = function(v,a,u)
        u = u or vec3(1,0,0)
        u = u:normalize()
        local x = v - v:dot(u)*u
        local y = u:cross(x)
        return v - x + math.cos(a)*x + math.sin(a)*y
    end
    
    function f(t)
        local r,h=5,5
        return vec3(r*math.cos(t),r*math.sin(t),h*t)
    end
    

    The way it works is to create a table of points along the path (this version uses the parametrisation - the one used in the Roller Coaster uses arc length but that's harder to compute). It also computes the tangent vector at each point, and it keeps track of a normal vector (the method by which it does that is slightly complicated). These vectors can then be used to construct a cylindrical segment from each point to the next with axis along the curve. The cylindrical segments can have end faces not parallel so it can cope easily with the curvature of the curve.

    I used a helix rather than a bézier curve for no good reason.

  • edited February 25 Posts: 1,045

    @Loopspace - problem with cimport, which can’t find in the code above (apart from the call) or in the libraries you’ve mentioned. Is it part of Craft code?

    Craft and Cameras are used as dependencies.

  • Posts: 338

    @Bri_G add the pseudomesh and meshext classes in your tabs, then comment out the cmodule line. Thanks @LoopSpace works great!...

  • Posts: 422

    @Bri_G Sorry, the line with cimport should have been removed before pasting. I use @toadkick's cmodule for side-loading tabs from other projects. I meant to remove all references to it when I copied the code here but one slipped through, sorry.

    @piinthesky thanks for posting the video! Do feel free to ask anything about the code.

  • Posts: 1,045

    Hi guys, sorry to be a pest on this but - I was using PseudoMesh an MeshExt and I even added the VecExt to try to get it working. Commented out the cimport but still no joy. Removed VecExt then moved PseudoMesh tab to left then no errors, but just a black screen. Left for about 30 secs still no image. I’m missing something.

  • dave1707dave1707 Mod
    Posts: 7,195

    @Bri_G I loaded it and it works fine. Image pops up in about a second. I reloaded the code @LoopSpace posted above. I put PseudoMesh in a tab called PseudoMesh. I put MeshExt in a tab called MeshExt. When I ran it I got an error and I think I had to take the local off of PseudoMesh = class(). Post again if it doesn’t work and I’ll delete it and try loading all of it again and see what happens.

  • Posts: 1,045

    Can’t understand this, recouped all three tabs, with and without local on the PseudoMesh = class(). Bother dependencies on and main tab in middle. Just a black screen.

    Have just updated to iOS 11.26 - no effect.

    I noticed that Codea was in the update store yesterday, I didn’t install and now it’s gone. Will try to re-install Codea and see if that resolves it

  • Posts: 1,045

    @Simeon - just noticed a funny on my iPad, held my touch on Codea icon and it brought up several images from my Dropbox, pressed see more and it showed more images. This occurred after doing a backup to my Dropbox.

  • edited February 26 Posts: 1,045

    Can’t understand this, repasted all three tabs, with and without local on the PseudoMesh = class(). Bother dependencies on and main tab in middle. Just a black screen.

    Have just updated to iOS 11.2.6 - no effect.

    I noticed that Codea

  • dave1707dave1707 Mod
    Posts: 7,195

    @Bri_G I loaded everything again and it worked. So here’s the steps I took.

    1. Copied the code from above and pasted it into a new project.
    2. Went to the Graphics Library shown above and opened PseudoMesh.lua
    3. Selected RAW and copied the code.
    4. Created a new blank file (tab) and named it PseudoMesh
    5. Pasted the PseudoMesh code into the tab
    6. Went to the Graphics Library and opened MeshExt.lua
    7. Selected RAW and copied the code
    8. Created a new blank file and named it MeshExt
    9. Pasted the MeshExt code into the tab
    10. Ran the code.
    11. Got error for Craft and Cameras, included them
    12. Got error for PseudoMesh, removed local from PseudoMesh=class()
    13. Ran program and image appeared in about 1 second.

    If no errors and nothing shows, put 2 fingers on the screen and move them together. Maybe the image is off to one side and pinching the screen will move the camera farther from the image and maybe it will show.

  • dave1707dave1707 Mod
    Posts: 7,195

    @Bri_G As for long pressing on the Codea icon, I mentioned that in the Codea 103 discussion. For me, it shows files from the Files app.

  • Posts: 422

    Here's the code in one file, with most of the unnecessary stuff removed.

    https://gist.github.com/loopspace/094ef466b0066fe3c5969a9960c3e694

  • edited February 26 Posts: 1,045

    @Loopspace, @Dave1707 - thanks for the help on this, uninstalled Codea then reinstalled and synced my Dropbox - an hour later followed @Dave1707 stepwise breakdown - still got an error, moved PseudoMesh before Main and lost error but just a black screen - just like before.

    Copied @Loopspace code across and ran perfectly - very impressive and very fast. Thank you.

    On an aside, after dumping all my code I started to set up collections to tidy up the code when I re-install all projects and noted @Simeon has added collapsible collections - thank you.

    Are these structures retained in a backup?

  • dave1707dave1707 Mod
    Posts: 7,195

    @Bri_G What do you mean by collapsible collections. How, where?

  • Posts: 1,045
    @dave1707 - when you use fileapp to look at the Codea folder in 103 things seem to have changed, but I need to check a few things first.

    One thing that I think is new is if you enter project folders(called collections) by creating folders in Fileapp e.g. utilities.collection then you will get a > symbol followed by that title in Fileapp. Whilst in Fileapp you can transfer projects into the utilities.collection and then see them on the Codea Project page by clicking on the > utilities.collection entry. This allows you to tidy up your projects, but it also makes it less onerous to scroll through the projects as, by clicking on the > utilities.collection you can alternately open and collapse that collection.

    I've set a 1Dependencies collection up and put the cameras in it, hoping I could see it reflected in the Dependencies list but no joy. I think we need the same management system on the Dependencies.

    Sorry about the delay in replying - had to recharge my pad.
  • dave1707dave1707 Mod
    Posts: 7,195

    @Bri_G So it’s the Files app. I thought it was something new in Codea that I didn’t see. What would be nice is selecting a Codea project in the Files app, it switches to Codea and opens that project, lets you do whatever you want and when you close the project, it goes back to the Files app where you started. I don’t use the Files app for anything right now, but that might make things a little easier for those that want to group their projects.

  • Posts: 1,045

    @dave1707 - it’s like I said, useful if you want to organise your projects - especially if you have many live projects. But you do need Backup to record the collection structure with the files to make this a really useful tool. Otherwise you are going to spend a lot of time shuffling projects.

  • dave1707dave1707 Mod
    edited March 2 Posts: 7,195

    Here’s a variation to my code above that drew 20 Bézier curves using Craft spheres. This version draws tubes instead of the spheres. Some changes made by @LoopSpace. It takes several seconds for the image to show.

    displayMode(FULLSCREEN)
    
    function setup()
        diameter=6
        sides=10
        assert(craft, "Please include Craft as a dependency")
        assert(OrbitViewer, "Please include Cameras (not Camera) as a dependency")        
        scene = craft.scene()
        v=scene.camera:add(OrbitViewer,vec3(0,0,0), 700, 0, 2000)
        v.camera.farPlane=3000
        v.ry=60
        for z=1,20 do
            pos,ind,col,nor={},{},{},{}
            bezier()
            createSkin()
            createTube()
        end
        createSphere(vec3(0,0,-200),20) --  starting red sphere
        createSphere(vec3(0,0,200),20)  --  ending red sphere  
    end
    
    function draw()
        update(DeltaTime)
        scene:draw() 
        text("Slide your finger around the screen to rotate the image.",WIDTH/2,HEIGHT-25)
        text("Use two fingers to zoom in, zoom out or to move the image.",WIDTH/2,HEIGHT-50)
    end
    
    function update(dt)
        scene:update(dt)
    end
    
    function bezier(cnt)
        local val=400
        local tab1={}
        local x0 = vec3(0,0,-200)
        local x1 = vec3(math.random(-val,val),math.random(-val,val),math.random(-val,val))
        local x2 = vec3(math.random(-val,val),math.random(-val,val),math.random(-val,val))
        local x3 = vec3(0,0,200)
        local xt
        for t=0,1.01,.02 do
            xt = (1-t)^3*x0 + 3*t*(1-t)^2*x1 + 3*t^2*(1-t)*x2 + t^3*x3         
            table.insert(tab1,xt)
        end
        for z=1,#tab1-1 do
            createLoop(tab1[z],tab1[z+1],false) -- createLoop on 1st to next to last point
        end
        createLoop(tab1[#tab1-1],tab1[#tab1],true)  -- createLoop on last point
    end
    
    function createLoop(p1,p2,last)
        local rp=vec3(1,1,1)
        local v1=rp-p1
        local r1=v1:cross(p2-p1)
        local s1=r1:cross(p2-p1)
        r1,s1=r1:normalize(),s1:normalize()    
        local n
        for a=0,359,360/sides do
            n = r1 * math.cos(math.rad(a)) + s1 * math.sin(math.rad(a))
            n=n*diameter
            if last then
                table.insert(pos,n + p2)    -- add p2 to last loop
            else
                table.insert(pos,n + p1)    -- add p1 to loop
            end
            table.insert(nor,n)
        end
    end
    
    function createSkin()
        o,p={1,2,3,4,5,6,3,2,1,6,5,4},{}
        for z=1,#pos-sides do
            p[1],p[2],p[3],p[4],p[5],p[6]=z,z+1,z+sides+1,z,z+sides+1,z+sides
            if z%sides==0 then
                p[2]=z-sides+1
                p[3]=z+1
                p[5]=z+1
            end
            for t=1,12 do
                table.insert(ind,p[o[t]])
            end
        end 
        for z=1,#pos do
            if (z-1)%1000==0 then   -- change color on each tube
                c=color(math.random(255),math.random(255),math.random(255))
            end
            table.insert(col,c)
        end
    end
    
    function createSphere(p,s)
        local pt=scene:entity()
        pt.position=p
        pt.model = craft.model.icosphere(s,2)
        pt.material = craft.material("Materials:Specular")
        pt.material.diffuse=color(255,0,0)
    end
    
    function createTube()
        local pt=scene:entity()
        pt.model = craft.model()
        pt.model.positions=pos
        pt.model.indices=ind
        pt.model.colors=col
        pt.model.normals=nor
        pt.material = craft.material("Materials:Specular")    
    end
    
  • dave1707dave1707 Mod
    Posts: 7,195

    Corrected some code and removed some code to improve the above program.

Sign In or Register to comment.