GenAI and Intersecting Polygons

Writing code with GenAI's help is not easy, and there are several "gotchas" that can make it difficult to use. It's important to understand the limitations of GenAI. While I am bullish on GenAI's future, it's not perfect.

GenAI and Intersecting Polygons

Writing code with GenAI's help is not easy, and there are several "gotchas" that can make it difficult to use.

As well, it's important to understand the limitations of GenAI. While I am bullish on GenAI's future, it's not perfect; depending on your perspective, it could be considered to have massive limitations. Or maybe not. However, like many technologies, it is important to use it with a heavy dose of caution.

For example, you could make statements about GenAI like the ones below and not be incorrect, at least not at this point in time.

  • It's untethered from reality
  • It has unpredictable reasoning (if it is reasoning at all)
  • It has a very limited understanding of the world
  • It will not be aware of things that are not in the corpus it was trained on
  • It will be confident in its answers, even when it's wrong
  • It will give different answers to the same question at different times

As long as we know these things, using GenAI becomes easier and safer, and in some ways can even be fun, a way to learn, a pedagogical tool.

Intersection of Two Polygons

An example of where it can go wrong is asking it to write something in Python where the Python code may not be in the training corpus, i.e., perhaps no one ever asked anyone how to do it in Python on Stackoverflow, there's no solution on Github, and it's not in the Python documentation or other related libraries.

We can ask it to do something somewhat esoteric, like write a function that calculates the intersection of two polygons, the example that Filip Piekniewski gives.

PROMPT> Write a function that calculates the intersection of two polygons. The function is given two polygons represented as a list of points. A third polygon representing the intersection of the two is returned, or an empty list of points if the intersection is empty.

Here is the code and responses from ChatGPT 4.

CHATGPT 4 RESPONSE> To calculate the intersection of two polygons, you can use computational geometry algorithms. One of the most common ways to find the intersection of two polygons is by using the Shapely library in Python. Shapely is a BSD-licensed Python package for manipulation and analysis of planar geometric objects, which is very well-suited for this kind of task.*

Here's an example of how you might write a function to find the intersection of two polygons using Shapely:

CHATGPT 4 RESPONSE CONT’D> This function first converts the lists of points into Polygon objects, then calculates their intersection. If the intersection is not empty, it returns the list of points representing the intersection polygon. Note that the function assumes the input polygons are valid (i.e., they are closed and do not self-intersect).

Remember that computational geometry can get quite complex, especially for polygons with many vertices or complex shapes. The Shapely library handles many of these complexities for you.

This is the output from the code above.

As well, I asked it to write code to generate an image of the polygon intersection, and below is the image it generated. (That graph code is not included here for brevity.)

In this example, ChatGPT 4 simply uses the Shapely library to solve the problem using the convenient intersection method. It does produce a polygon shape, representing the overlap or intersection of the two squares provided. However, in Piekniewski's example, he did not achieve the desired results using a different GenAI tool.

If we ask ChatGPT 3.5 as opposed to ChatGPT 4, we may not get the same answer; it may not know the Shapely library, and it may not know how to do this in Python, or it may simply make other choices.

Here I present the same prompt to ChatGPT 3.5, and this is the information and code it provided.

CHATGPT 3.5 RESPONSE> Calculating the intersection of two polygons can be a complex task, especially if the polygons have more than four vertices or are self-intersecting. One common approach is to use computational geometry libraries or functions, but I can provide you with a basic Python function to calculate the intersection of two convex polygons. Keep in mind that this solution assumes convex polygons and may not work correctly for more complex cases.

Here's a Python function to calculate the intersection of two convex polygons represented as lists of points:

CHATGPT 3.5 RESPONSE CONT’D> Please note that this function assumes that the input polygons are convex and may not work correctly for non-convex polygons or polygons with self-intersections. For more complex cases, consider using specialized computational geometry libraries like Shapely or GEOS.

I changed the code it provided slightly, just to provide it with the same polygon shapes as the ChatGPT 4 provided code which we know from the image do intersect. Otherwise I left the code alone.

The ChatGPT 3.5 output is incorrect.

It does mention the Shapely library, which I believe it should have used, but instead of using it, ChatGPT 3.5 tries to write its own algorithm, which fails; certainly, it doesn't produce the same results as the ChatGPT 4 example, which I feel are correct, and what I asked for in the prompt.

In the end, we have to be careful when using GenAI to write code, and it may turn out that this is a better pedagogical tool than a production tool, in that in order to ensure that it is working correctly, you have to understand the code it generates. In this case, you might have to know what a polygon intersection is, how it works, and perhaps even what the Shapely library is doing behind the scenes. That said, if it seems like no one has ever written a script to do this in Python, then 1) it's probably because Shapely does it, and 2) it's just not a common thing to do, say when a company is simply writing REST APIs.

Subscribe to Tidal Series by Curtis Collicutt

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe