Skip to main content
gamedev.net
🔒 Locked

Polygon border - a different approach

Started by andy82 Feb 14, 2007 at 11:26 AM 4 replies 4.6k views
Original Post
andy82
andy82
Hi, although the method discussed here here works somewhat satisfactory now, I'm still thinking if there could a better method to achieve the following aesthetic: (I want to draw a border around an arbitrary polygon; in this example the source polygon is the red arrow) What came to my is the following: Could I not simply do two drawing steps? 1) Draw the filled red polygon 2) Then draw the border around the red polygon using thick lines The only problem now is that the thick lines must join together nicely. I could do this by giving them round edges (draw a circle of radius "line_thickness" at each end) but I think it would be much better if the lines had sharp joints. Here's an illustration (the first poly lines use sharp joints, the second poly lines use round joints): So my question would be: How to achieve the first aesthetic? How do I connect two thick line segments so that it looks nicely just as in the first example in the illustration above? I don't really have an idea how it could be done. Also, to the nice people who have followed my other threads on this polygon border topic: Would this thick line-approach solve the floating point accuracy problems which we have discussed in the other threads? Thanks for your help. Andy
Fingers_
Fingers_
As far as I can tell this is the same border algorithm you were doing before, only you draw it as a strip around the original polygon rather than overdrawing the original on top of an inflated polygon. So yes, this can be done.

Assuming the inflated polygon has the same number of vertices as the original (ie. sharp corners), this is very easy. Say you have a 4-sided polygon with vertices "abcd" in a counter-clockwise order. set up the inflated vertices "ABCD" using the method of choice. Then just draw a triangle strip between them, ordered like this: aAbBcCdDaA.
honnyjopper
honnyjopper
yeah the old version does seem slightly overly complex. to get the effect you describe is pretty easy, especially in 2d.

it's easiest describe in pseudo code, something like this (obviously the poly has > 2 verts :) )

//start a triangle strip off in whatever API you're using//assuming a handy v2 structure here...StartTriangleStrip();v2 cur, prev, next;prev = poly[poly.size()-1];cur = poly[0];next = poly[1];for (int i=0;i<poly.size();++i){  v2 cur_edge = (next-cur).Normal();  v2 prev_edge = (cur-prev).Normal();  v2 out = (cur_edge+prev_edge).Normal().Right();  AddAVertex(cur+out); //this does whatever  AddAVertex(cur-out);   prev = cur;  cur = next;  next= poly[(i+1)%poly.size()];}DrawTriangleStrip();


and that's it. you might find you need a special case for when the angle is too sharp (to make it look nice), maybe round off the corner or something.

edit: normalise cur_edge and prev_edge

[Edited by - honnyjopper on February 15, 2007 4:59:15 AM]
honnyjopper
honnyjopper
oh and obviously multiply the "out" vector by half the fatness of the border you want.

double p.s., i have assumed that the polygon is already sorted... if not this won't work obviously. sort your poly!
Todd Smith
Todd Smith
You could draw the original arrow scaled up a bit in all black then render the red arrow on top of it.
TheAdmiral
TheAdmiral
Quote:
Original post by Fingers_
As far as I can tell this is the same border algorithm you were doing before, only you draw it as a strip around the original polygon rather than overdrawing the original on top of an inflated polygon.

The existing method leaves the polygon untouched, creating a border on the exterior. Drawing a fat perimeter along the edges as suggested will obscure some of the polygon.

Quote:
Original post by Todd Smith
You could draw the original arrow scaled up a bit in all black then render the red arrow on top of it.

This was an early suggestion that led to andy82's current implementation. The problem is in the definition of 'scaled up'. A uniform scale will give stupid results for non-uniformly-shaped polygons. This doesn't get much better when you normalise out the scaling factors, and still stands no chance on a convex polygon. In fact, in order to make the border entirely uniform, you need to scale the vertices individually, along their 'normalised & pre-normalised' normals. This is exactly the implementation we ended up at in the last thread.

Back onto the topic, I can't imagine there's any way to draw a thick border that has non-orthogonal edges without going all the way through the per-vertex analysis again. In particular, to find the terminal point on the exterior of each edge, you'd need to extrapolate along the normals as you already are. So unless I've made a grave error in judgement, I'm afraid you'll need to settle for rounded-edges or leave things as they are.

Quote:
Original post by andy82
Also, to the nice people who have followed my other threads on this polygon border topic: Would this thick line-approach solve the floating point accuracy problems which we have discussed in the other threads?

There's no way to 'solve' the floating inaccuracy issue - using a tolerance as I described is very standard, and works great - but if your method needs no equality tests then you'll have sidestepped the issue altogether. If you were to implement the 'rounded edge' solution then this would be the case.

Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.