Geometry Library-WIP

Hello all,
Thought some of you out there might find this useful. It is a compilation of all of the geometry related functions I use in my projects. It also might help some of you out there familiar with processing with Codea 3d, as it makes coding ‘boxes’ similar to in processing. If there are any functions any of you would like me to add, you can leave them in the comments below.
PS-I know I am doing the get angle wrong, but it works
PPS-I know there is a distance and an angle function already, but I trust mine more

TO USE:
Copy and paste into a new project, called something like geometry library. Then, whenever you want to use it in a project, hit new tab and then import it as a dependancy. I make it sound harder than it is here.
CODE:


--# Geo
Geo = class()

function Geo:init(x)
    -- you can accept and set parameters here
    self.x = x
end

function Geo:draw()
    -- Codea does not automatically call this method
end

function Geo:touched(touch)
    -- Codea does not automatically call this method
end

function box(x, y, z, width, height, depth, col)
    local m=mesh()
    
    m.vertices={
    --front
    vec3(x+width/2, y+height/2, z+depth/2),
    vec3(x-width/2, y+height/2, z+depth/2),
    vec3(x+width/2, y-height/2, z+depth/2),
    
    vec3(x-width/2, y+height/2, z+depth/2),
    vec3(x-width/2, y-height/2, z+depth/2),
    vec3(x+width/2, y-height/2, z+depth/2),
    --right side
    vec3(x+width/2, y+height/2, z+depth/2),
    vec3(x+width/2, y-height/2, z+depth/2),
    vec3(x+width/2, y+height/2, z-depth/2),
    
    vec3(x+width/2, y-height/2, z+depth/2),
    vec3(x+width/2, y-height/2, z-depth/2),
    vec3(x+width/2, y+height/2, z-depth/2),
    --left side
    vec3(x-width/2, y+height/2, z+depth/2),
    vec3(x-width/2, y-height/2, z+depth/2),
    vec3(x-width/2, y+height/2, z-depth/2),
    
    vec3(x-width/2, y-height/2, z+depth/2),
    vec3(x-width/2, y-height/2, z-depth/2),
    vec3(x-width/2, y+height/2, z-depth/2),
    --back
    vec3(x-width/2, y+height/2, z-depth/2),
    vec3(x-width/2, y-height/2, z-depth/2),
    vec3(x+width/2, y+height/2, z-depth/2),
    
    vec3(x+width/2, y-height/2, z-depth/2),
    vec3(x+width/2, y+height/2, z-depth/2),
    vec3(x-width/2, y-height/2, z-depth/2),
    --bottom
    vec3(x+width/2, y-height/2, z+depth/2),
    vec3(x+width/2, y-height/2, z-depth/2),
    vec3(x-width/2, y-height/2, z+depth/2),
    
    vec3(x-width/2, y-height/2, z-depth/2),
    vec3(x-width/2, y-height/2, z+depth/2),
    vec3(x+width/2, y-height/2, z-depth/2),
    --top
    vec3(x-width/2, y+height/2, z+depth/2),
    vec3(x+width/2, y+height/2, z+depth/2),
    vec3(x-width/2, y+height/2, z-depth/2),
    
    vec3(x-width/2, y+height/2, z-depth/2),
    vec3(x+width/2, y+height/2, z-depth/2),
    vec3(x+width/2, y+height/2, z+depth/2)
    }
    if type(col)=="userdata" then
       m:setColors(col)
    end
    if type(col)=="table" then
        m.colors={
        col[1],
        col[1],
        col[1],
        col[1],
        col[1],
        col[1],
        
        col[2],
        col[2],
        col[2],
        col[2],
        col[2],
        col[2],
        
        col[3],
        col[3],
        col[3],
        col[3],
        col[3],
        col[3],
        
        col[4],
        col[4],
        col[4],
        col[4],
        col[4],
        col[4],
        
        col[5],
        col[5],
        col[5],
        col[5],
        col[5],
        col[5],
        
        col[6],
        col[6],
        col[6],
        col[6],
        col[6],
        col[6]
        }
    end
    m:draw()
end

function tri(x1, y1, x2, y2, x3, y3, col)
    local m=mesh()
    m.vertices={
    vec3(x1, y1),
    vec3(x2, y2),
    vec3(x3, y3)
    }
    m:setColors(col)
    m:draw()
end

function quad(x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4)
    local m=mesh()
    local pointOne=vec3(x1,y1, z1)
    local pointTwo=vec3(x2,y2, z2)
    local pointThree=vec3(x3,y3, z3)
    local pointFour=vec3(x4,y4, z4)
    m.vertices={
    pointOne,
    pointTwo,
    pointThree,
    pontOne,
    pointThree,
    pointFour
    }
    m:draw()
end

function vecDistance(pos1, pos2)
    local dis=math.sqrt((pos1.x-pos2.x)^2 + (pos1.y-pos2.y)^2)
    return dis
end

function vecAcc(pos1, pos2)
    local mag=vecDistance(pos1, pos2)
    local dif=pos1-pos2
    dif = dif / mag
    return dif
end

function angleBetween(pos1, pos2)
    local angle=-math.deg(math.atan2((pos1.x-pos2.x),(pos1.y-pos2.y)))
    return angle
end

--# Main
-- Geometry library

-- Use this function to perform your initial setup
function setup()

end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness
    strokeWidth(5)
end

Sorry, it is a bit long. :smiley:

@TheSolderKing - I’m a bit puzzled. I can only see three geometric functions, all of which already exist in Codea, and the rest of the code is creating a kind of mesh. :-?

Sorry for the confusion Ignatz, this code is simply meant to serve as a perhaps more understandable way of calling some of the functions codea gives you like angleBetween, distance, and acc. The other three, based on meshes(quad, tri ,and box) make creating 3d objects a lot easier. Sorry again for the confusion.

ok, but I don’t see any point in writing your own versions of existing functions, because they will be slower - and I don’t understand “I trust mine more”. Are you suggesting the built in functions are wrong? Have you actually found an error?

I suggest you rather use the built in functions and put in a comment with the formula if you want to explain how it works.

If you want to make your code more compact, I suggest creating local variables, eg for the box

local x1,y1,z1=x-width/2, y-height/2, z-depth/2
local x2,y2,z2=x+width/2, y+height/2, z+depth/2
--then your vertex code becomes much more compact (and readable)
    --front
    vec3(x2, y2, z2),vec3(x1, y2, z2),vec3(x2, y1, z2),

@Ignatz Adding on to code compression - the massive table of colors could probably be compressed to 3 lines using a for loop.

Sorry @Ignatz, I really should have used “Understand more” rather than “trust mine more,” because that is really my purpose for coding them. @SkyTheCoder and @Ignatz, I will implement your code compression shortly. Thank you! :smiley: