concept polygon in category python

appears as: polygon, polygons, polygons, polygon, The polygon, A polygon
Math for Programmers: 3D graphics, machine learning, and simulations with Python MEAP V11

This is an excerpt from Manning's book Math for Programmers: 3D graphics, machine learning, and simulations with Python MEAP V11.

Table 2.1: Some Python classes representing geometric figures, usable with the draw function.

Class

Constructor example

Description

Polygon

Polygon(*vectors)

Draws a polygon whose vertices (corners) are represented by a list of vectors.

Points

Points(*vectors)

Represents a list of points (dots) to draw, one at each of the input vectors.

Arrow

Arrow(tip)

Arrow(tip, tail)

Draws an arrow from the origin to the tip vector or from the tail vector to the head vector if a tail is specified.

Segment

Segment(start,end)

Draws a line segment from the start to the vector end.

The dinosaur drawn as a polygon.

With the techniques from the last two chapters and a little creativity, you can render any 2D or 3D figure you can think of. Whole objects, characters, and worlds can be built from line segments and polygons defined by vectors. But, there’s still one thing standing in between you and your first feature-length, computer-animated film or life-like action video game—you need to be able to draw objects that change over time.

def polygon_map(transformation, polygons): 
      return [ 
          [transformation(vertex) for vertex in triangle] 
          for triangle in polygons 
      ]
Geoprocessing with Python

This is an excerpt from Manning's book Geoprocessing with Python.

You’ll learn how to work with the two main types of spatial data, vector and raster. Vector data is made up of points, lines, and polygons, while raster data is a two- or three-dimensional array of data values such as the pixels in a photograph. A dataset containing country boundaries is an example of vector data. In this case, each country is generally represented as a polygon. Datasets that use lines to represent roads or rivers, or points to show the location of weather stations, are other examples. Early primitive maps, such as those drawn on cave walls, only showed the features themselves. Later maps contained labels for features of interest such as cities or seaports; for example, the Portolan map of northwest Africa shown in figure 1.1.

You have several kinds of geometries with which you can work, points being the simplest. All other types are made up of points connected by straight line segments, and the points are what store the coordinate values, so points can be thought of as the building blocks of geometries. These points that are used to build other geometries are called vertices, and there can be thousands of vertices per geometry, if needed. For example, line geometries are an ordered collection of points that are connected by straight line segments, with a vertex at every location where the line needs to change direction. A line representing a short dead-end street wouldn’t require many vertices, but one representing the Amazon River in much detail would need thousands. Polygons are somewhat similar to lines, but they’re closed, meaning the first and last vertex are identical and they enclose a specific area. You’ll start with creating and editing points and work your way up to the more complicated geometries as you go along.

A polygon is like a multigeometry in the sense that it consists of a set of geometries. All polygons are made of rings, which in turn are made of vertices. A simple polygon only has one ring, but you still need to create a ring object and then add it to the polygon. As with lines, use AddPoint to add a vertex to a ring. Vertices need to be added in order, but the direction around the perimeter can vary depending on the format you want to use to store the data. For example, shapefiles specify that the outer rings are in clockwise order, but GeoJSON doesn’t specify an order. Because of details like this, it’s probably a good idea to read up about the format you intend to use. But no matter the direction, the first and last vertices must have the same coordinates so they close the ring. To do this, you can either make sure the last vertex added has identical coordinates to the first one, or you can call CloseRings on the ring or polygon after adding all vertices. The latter method is the one used here to create the yard outline shown in figure 6.16. The example starts with the upper left vertex and traverses the perimeter in counter-clockwise direction.

Creating lines from polygons

Sometimes you need to convert polygons to lines. To do this, you need to create a line from each ring inside the polygon. I’ve had luck copying the rings into line features, but the following listing shows how to do it by creating a line from a ring instead. Similarly to the line_to_point_layer function in listing 6.1, this function requires a data source, the name of the existing polygon layer, and the name for a new line layer. It creates a new line layer with the same attributes as the polygon layer, and then for each polygon feature, copies each ring to a line and inserts a new feature in the line layer.

Listing 6.2. Function to create a line layer from a polygon layer

After creating a new layer to hold the lines, the function starts iterating through the polygons in the original layer, and creates a new line for each ring in the polygon. To do this, each time it finds a ring, it creates an empty line object and then iterates through the ring’s vertices. The coordinates for each ring vertex are used to create a new vertex in the line, so you get a line containing all of the same vertices as the ring. Lines, however, aren’t closed even if the first and last vertices are the same. If you plot the line, it will look like a polygon, but it doesn’t have the concept of an area or anything else specific to polygons.

sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest