I'm trying to post this question to SO, but it keeps complaining that it

contains code that is not properly formatted as code

and I can't figure out why. Am I missing something, or is there a bug in the code-detection heuristic?

I've tried wrapping suspect phrases in inline code blocks with backticks, but to no avail. My question's source is:

I have a 2D plane, partitioned into n-sided, convex polygons. I'm using [WRF's PNPOLY algorithm][PNPOLY] for polygon inclusion to ensure that a point belongs inside one and only one polygon.

Is there an algorithm I can use to clip a line segment **PO** to a given polygon in the plane, assuming that `pnpoly(O) == true`, such that **pnpoly(P')** will always be true?

![polygon-segment clipping diagram][diagram]

My current implementation of clipToPoly does a line-line intersection test with the segment and each edge of the polygon, then uses the intersection point (as detailed in [this SO answer][intersection]), but this does not always yield a point that satisfies PNPOLY.

<!-- language: lang-js -->

    clipPointToPoly = function(p, o, poly) {
        var i, j, n = poly.length,
            q, r = {}, s = {}, pq = {},
            rxs, t, u;

        function cross2(v, w) {
            return v.x * w.y - v.y * w.x;

        for (i = 0, j = n - 1; i < n; j = i++) {
            q = poly[i];
            s.x = poly[j].x - q.x;
            s.y = poly[j].y - q.y;

            r.x = o.x - p.x;
            r.y = o.y - p.y;

            rxs = cross2(r, s);
            if (rxs !== 0) {
                pq.x = q.x - p.x;
                pq.y = q.y - p.y;

                t = cross2(pq, s) / rxs;
                u = cross2(pq, r) / rxs;
                if (0 < u && u < 1 && 0 < t && t < 1) {
                    p.x = p.x + t * r.x;
                    p.y = p.y + t * r.y;
                    return true;
        return false;

I don't think I understand enough about the Simulation of Simplicity, or how to deal with PNPOLY's half-open sets when using floating point numbers to handle the edge cases properly.

**For example:**

    poly: [(1,1), (-1,1), (-1,-1), (1,-1)]
    p: (5,5)
    o: (0,0)
    p' = (1,1)

This fails because (1,1) is not included according to PNPOLY (it's on the open side of the set), but clipToPoly does not take that into account. I suppose I could nudge it by an epsilon if I knew it was on an open end of the set, but I'd prefer a more stable solution.

**Another example:**

    poly: [-995.9592341908675, -88.48705014724577
     -1040.5031753180106, -176.53192722405026
     -549.9211095905894, -330.8462151682281
     -653.7143990581328, -211.59193148034612]
    p: -1032.3773586525654, -208.3586379393678
    o: -957.4172402148379, -202.6668958854324

In this case, clipToPoly fails because **O** is so close to the edge of the polygon, it doesn't even detect an intersection due to floating point imprecision.

    t: 1.0000000000000002 u: 0.8306380503739466

Is there a way to get clipToPoly's floating point imprecision to match PNPOLY's, so that both are consistent?

[diagram]: http://i.stack.imgur.com/aI6Y2.png
[PNPOLY]: http://www.ecse.rpi.edu/~wrf/Research/Short_Notes/pnpoly.html
[intersection]: http://stackoverflow.com/a/565282/382431
  • 6
    1. I hate scrolling horizontally. 2. I hate scrolling vertically. – juergen d Aug 5 '13 at 17:18
  • 1
    @juergen d: You can copy the input, paste it into the Answer field below, and see the live preview. You won't see any scrollbars then. – BoltClock's a Unicorn Aug 5 '13 at 17:19
  • Try posting it without indenting the paragraphs. Stack Overflow tries to format anything indented 4 spaces as code. – Bill the Lizard Aug 5 '13 at 17:21
  • 3
    @juergend I don't understand how I am supposed to post the source code for markdown without placing that code in a code block, thus requiring horizontal scrolling. And yes, 'source code' for markdown seems a bit odd, but that's essentially what it is. – JDS Aug 5 '13 at 17:22
  • 2
    @Bill the Lizard: None of the paragraphs are being indented - the only things I see being indented are code snippets and output. – BoltClock's a Unicorn Aug 5 '13 at 17:25
  • @BoltClock'saUnicorn Weird. I see 4 spaces at the beginning of every paragraph except the first one. – Bill the Lizard Aug 5 '13 at 17:26
  • 2
    @Bill the Lizard: I'm gathering that's because the OP is providing the raw Markdown he's used to post the question, and that itself has to be indented for the purposes of this meta question. – BoltClock's a Unicorn Aug 5 '13 at 17:27
  • 2
    @juergend If I modify the text that's failing the SO code-detection parser, then it is no longer the text that's failing the SO code detection parser. – JDS Aug 5 '13 at 17:28
  • 1
    @JDS: Yes, you're right. I did not get it at first. – juergen d Aug 5 '13 at 17:29

The problem is with the Markdown image link

![polygon-segment clipping diagram][diagram]
[diagram]: http://i.stack.imgur.com/aI6Y2.png

The named reference-link seems to be the culprit. If I change it to a number, it passes the test:

![polygon-segment clipping diagram][1]
[1]: http://i.stack.imgur.com/aI6Y2.png

This does seem to be a bug in the SO code-detection parser, where it incorrectly identifies image reference-links that use a string instead of a number as code.


@JDS Me too. The problem was also with the Markdown image link. I fixed the error below by simple deleting a link:

"Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon."

simple fix:

enter image description here ^- Above is the same article with only one link (Images.xml). not two links as before (ImagesMinimal.xml and Images.xml).

With this posting has worked here: How to fix Shopware images import error - Ordernumber and image are required

Here you could see the error again (red box):

enter image description here

Hope this helps.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .