I have a set of vertices/vectors and I need to extrude them inside object boundaries to give that object a thickness
as an example:
I need to turn something like this: to something like this:
how can I achieve this?
(I'm using C++, OpenGL & GLM)
-- Update --
thanks to @Futurologist answer, I was able to resolve the issue and hey, It works like a charm!
Sorry for the python, but it's faster and easier for me to write it like that, plus it maybe reveals some the geometric concepts in the background.
Is this what you are after?
'''
angle bisectors and offsetting a polygon
'''
def bisectors(P, thickness):
#P is n x 2 matrix, row P[j,:] is a vertex of a polygon in the plane,
#P is the ordered set of vertices of the polygon
n = P.shape[0];
B = np.zeros((n,2), dtype=float);
for j in range(n):
if j == 0:
v_in = P[0,:] - P[n-1,:];
v_out = P[1,:] - P[0,:];
elif j == n-1:
v_in = P[n-1,:] - P[n-2,:];
v_out = P[0,:] - P[n-1,:];
else:
v_in = P[j,:] - P[j-1,:];
v_out =P[j+1,:] - P[j,:];
v_in = v_in / math.sqrt(v_in.dot(v_in)); #normalize edge-vector
v_out = v_out / math.sqrt(v_out.dot(v_out)); #normalize edge-vector
# bisector of the complementary angle at the vertex j,
# pointing counter clockwise and displacing the vertex so that
# the resulting polygon is "thickness" units inwards in normal direction:
bisector = v_in + v_out;
bisector = bisector / abs(bisector.dot(v_in));
bisector = thickness * bisector
# 90 degree counter clockwise rotation of complementary bisector:
B[j,0] = - bisector[1];
B[j,1] = bisector[0];
return B
def offset_vertices(Polygon, thickness):
Polygon_off = Polygon + bisectors(Polygon, thickness)
return Polygon_off
P = np.array([[0,0],[2,0],[3,1],[1,3]])
P_off = offset_vertices(P, 0.1)
# Plotting
P = np.vstack((P, P[0,:] ))
P_off = np.vstack((P_off, P_off[0,:] ))
fig, axs = plt.subplots(1)
axs.plot(P[:,0], P[:,1], 'bo')
axs.plot(P_off[:,0], P_off[:,1], 'ro')
axs.plot(P[:,0], P[:,1])
axs.plot(P_off[:,0], P_off[:,1])
axs.set_aspect('equal')
plt.grid()
plt.show()