Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8540817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:41:18+00:00 2026-06-11T11:41:18+00:00

I am creating a polygon using Google Maps V3 API. It starts off with

  • 0

I am creating a polygon using Google Maps V3 API. It starts off with a 4 sided polygon which is a square.

Problem: Now when a new point is added to the polygon, it is possible for the polygon to overlap itself in a weird shape instead of forming a pentagon. This is because the polygon is drawn in the order the points are added to the array. Is it possible to rearrange the polygon points so no 2 lines overlap each other?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:41:19+00:00Added an answer on June 11, 2026 at 11:41 am

    I have successfully implemented Blazemonger’s suggestion above, and it works for me.

    Here was his novel suggestion:

    Another approach, which seems simpler to me: If the new point is (a)
    NOT in the interior of the existing polygon and (b) creates an
    interior angle greater than 180 degrees, then it must be invalid.
    There are many existing algorithms for testing whether a point is in
    the interior of a polygon (stackoverflow.com/q/471962/901048) and
    measuring the interior angle is a matter of finding the difference
    between the arctangents of each slope

    To determine if the point is in the polygon, I used the polygon.Contains function from Mike Williams’ updated epoly.js, found here: http://www.geocodezip.com/scripts/v3_epoly.js.

    If the point is not in the polygon (see above), we:
    1.) Determine if the interior angle of the last two vectors is greater than 180 degrees. To do so we need to implement the below equation to find the angle of last two vectors of the polygon:

    angleRadians = Math.acos((vx1 * vx2 + vy1 * vy2) / ( Math.sqrt( vx1*vx1 + vy1*vy1 ) * Math.sqrt( vx2*vx2 + vy2*vy2 ) ));

    this is using the Dot product of the vectors.

    But that does not take into account the ‘winding direction’, first you must get the cross product and, if the cross product is positive, it was a left turn, if negative–a right turn.

    I have included my JS code here, as a gist: https://gist.github.com/3741816.

    BTW, I’m a newbie here at StackOverflow — it’s been such a great resource to me thanks thanks to all who have contributed!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When drawing an editable Polygon on a map using Google's V3 API, is there
Using the Google Maps JavaScript v3 API's editable polygons and/or drawing library, is it
I'm creating a Google map mashup and am using SQL 2008. I will have
I'm creating a dialog with a single image, and a polygon overlaid. The problem
Environment: MacOSX, RoR 3.2.6, gmaps4rails 1.5.5, Google Maps API V3 Desire: It would be
I am using the Physics Editor for creating the Polygon in Box2d. It generates
I'm creating a map using the maps library and ggplot's geom_polygon. I'd simply like
I need to draw polygon using mouse and mark a particular area on Google
I am creating a polygon filled with texture using opengl in cocos2d framework. I
I am creating a web application on maps, using open source maps. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.