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