3
\$\begingroup\$

I have a set of vector3 coordinates and I use them to draw a polygon in another app, my question is: how can I calculate a second set of coordinates to make it look like the line is outlined.

an example of how it should look like at the end

in the above picture, the blue dots are the vector3s I have and the red dots are the one I need, can anyone help me with an algorithm or formula to calculate them?
thanks

\$\endgroup\$
  • \$\begingroup\$ It is offset @Ocelot, could you explain a little more, sorry \$\endgroup\$ – Henjin Oct 16 '16 at 6:20
  • \$\begingroup\$ Oh, god. Nevermind, I was wrong. \$\endgroup\$ – Ocelot Oct 16 '16 at 7:04
  • \$\begingroup\$ oh, :(, I was testing that. @Ocelot \$\endgroup\$ – Henjin Oct 16 '16 at 7:08
  • \$\begingroup\$ Don't loose hope, I think I figured it out. x) \$\endgroup\$ – Ocelot Oct 16 '16 at 7:09
  • \$\begingroup\$ Do you need outline to keep constant width irregardless of viewpoint and polygon shape (including concave) ? \$\endgroup\$ – Kromster says support Monica Oct 16 '16 at 9:05
3
\$\begingroup\$

1)Find barycentric coordinates

for(int i = 0; i < verticesInPolygon; i++)
{
    bary.xyz += vertices[i].xyz;
}
bary.xyz /= verticesInPolygon;

2)Create new vertices using this formula

newVertex.coord = (vertices[desiredVertexInPolygon].coord - bary.coord)*offset+bary.coord;

Where coord is a desired coordinate(x, y or z) and offset is a scalar determining how far from the barycenter and how close to the original vertex a new vertex will be, i. e. 0.5 would make it appear right between, making new polygon being a half size of the original.

illustration

UPDATE: So, you need a constant offset to make outline, right? Here is a way to do it.

1)Find barycentric coordinates

We already know how to do this.

2)Find a unit direction vector from barycenter to the original vertex

dir.xyz = normalize(vertices[desiredVertexInPolygon].xyz - bary.xyz);

3)Create new vertices with a following formula

newVertex.xyz = vertices[desiredVertexInPolygon].xyz + dir.xyz * offset;

Where offset is now a real distance rather than scalar.

enter image description here

Note that in your case you should use a negative offset value, because you want outline to be inside the polygon.

\$\endgroup\$
  • \$\begingroup\$ lets say I need the distance between the original vertex and the new vertex to be 0.2f, what should the offset be? \$\endgroup\$ – Henjin Oct 16 '16 at 7:23
  • \$\begingroup\$ @TheFallen 1.0 - 0.2 = 0.8? \$\endgroup\$ – Ocelot Oct 16 '16 at 7:25
  • \$\begingroup\$ well the problem with this is that based on the distance of the original point from the center, the distance of the new point from the original point varys, so the distance of the two lines are not the same across all of the shape, is there a way to fix it. sorry for the truble \$\endgroup\$ – Henjin Oct 16 '16 at 7:58
  • \$\begingroup\$ sorry but can you help me finish it? \$\endgroup\$ – Henjin Oct 16 '16 at 8:10
  • 1
    \$\begingroup\$ @TheFallen I'm wrong again. The correct way to do this is not trivial. The one method for this is using a Straight skeleton. en.wikipedia.org/wiki/Straight_skeleton \$\endgroup\$ – Ocelot Oct 16 '16 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.