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

Calculating Polygon Perimeter

Veeg om het menu te tonen

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?

Selecteer het correcte antwoord

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 5

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Sectie 1. Hoofdstuk 5
some-alt