Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Calculating Polygon Perimeter | Introduction to Geometric Modelling
Geometric Modelling with Python

Calculating Polygon Perimeter

Deslize para mostrar o menu

1. List the Vertices

A polygon is defined by a sequence of points (vertices) given as (x, y) coordinate pairs. For example, a triangle with vertices at (0, 0), (4, 0), and (4, 3) is represented as:

triangle = [(0, 0), (4, 0), (4, 3)]

2. Calculate Distances Between Consecutive Vertices

To find the length of each side, use the distance formula between two points:

distance = sqrt((x2 - x1)**2 + (y2 - y1)**2)

Loop through each pair of consecutive vertices and compute the distance.

3. Include the Closing Segment

After reaching the last vertex, connect it back to the first vertex. This ensures every side of the polygon is included in the perimeter calculation.

4. Sum All Distances

Add up all the distances to get the total perimeter.

For the triangle above:

  • Distance from (0, 0) to (4, 0) is 4;
  • Distance from (4, 0) to (4, 3) is 3;
  • Distance from (4, 3) back to (0, 0) is 5.

Total perimeter: 4 + 3 + 5 = 12.

By following these steps, you can calculate the perimeter of any polygon given its vertices in order.

123456789101112131415161718192021222324
from math import sqrt def polygon_perimeter(vertices): """ Compute the perimeter of a polygon given its vertices. Args: vertices (list of tuple): List of (x, y) tuples representing polygon vertices in order. Returns: float: Perimeter of the polygon. """ perimeter = 0.0 n = len(vertices) for i in range(n): x1, y1 = vertices[i] x2, y2 = vertices[(i + 1) % n] # Wrap around to the first vertex distance = sqrt((x2 - x1)**2 + (y2 - y1)**2) perimeter += distance return perimeter # Example usage: triangle = [(0, 0), (4, 0), (4, 3)] print("Triangle perimeter:", polygon_perimeter(triangle))
question mark

Which of the following statements about polygon representation and perimeter calculation in Python is correct?

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 5

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Seção 1. Capítulo 5
some-alt