Question

In: Computer Science

(Area of a convex polygon) A polygon is convex if it contains any line segment that...

(Area of a convex polygon)

A polygon is convex if it contains any line segment that connects two points of the polygon. Write a program that prompts the user to enter the number of points in a convex polygon, then enter the points clockwise, and display the area of the polygon.

Sample Run

Enter the number of points: 7

Enter the coordinates of the points:

-12 0 -8.5 10 0 11.4 5.5 7.8 6 -5.5 0 -7 -3.5 -5.5

The total area is 244.575

Solutions

Expert Solution

# (X[i], Y[i]) are coordinates of i'th point. 
def polygonArea(X, Y, n):
    # Initialze area
    area = 0.0

    # Calculate value of shoelace formula 
    j = n - 1
    for i in range(0, n):
        area += (X[j] + X[i]) * (Y[j] - Y[i])
        j = i  # j is previous vertex to i

    # Return absolute value
    return float(abs(area / 2.0))

#Taking number of points from user
points = int(input("Enter the number of points: "))
#Taking coordinates as a string
coordinates = input("Enter the coordinates of the points:\n")
#Splitting coordinates using split function and converting into list
cords = list(coordinates.split(" "))
X=[]
Y=[]
#Appending values into x and y lists
for i in range(len(cords)):
    if i%2==0:
        X.append(float(cords[i]))
    else:
        Y.append(float(cords[i]))
n = len(X)
print(polygonArea(X, Y, n)) 

Sample input and output:

Enter the number of points: 7
Enter the coordinates of the points:
-12 0 -8.5 10 0 11.4 5.5 7.8 6 -5.5 0 -7 -3.5 -5.5
244.575

#If you have any doubts please leave comment, THANK YOU.


Related Solutions

A polygon is called convex if every line segment from one vertex to another lies entirely...
A polygon is called convex if every line segment from one vertex to another lies entirely within the polygon. To triangulate a polygon, we take some of these line segments, which don’t cross one another, and use them to divide the polygon into triangles. Prove, by strong induction for all naturals n with n ≥ 3, that every convex polygon with n sides has a trian-gulation, and that every triangulation contains exactly n − 2 triangles. (Hint: When you divide...
A polygon is called convex if every line segment from one vertex to another lies entirely...
A polygon is called convex if every line segment from one vertex to another lies entirely within the polygon. To triangulate a polygon, we take some of these line segments, which don’t cross one another, and use them to divide the polygon into triangles. Prove, by strong induction for all naturals n with n ≥ 3, that every convex polygon with n sides has a triangulation, and that every triangulation contains exactly n − 2 triangles. (Hint: When you divide...
A convex polygon is defined as a polygon where all its internalangles are less than...
A convex polygon is defined as a polygon where all its internal angles are less than 180 degrees and no edges cross each other. You can assume the vertex with the smallest x-coordinate (assuming origin at bottom left) is the first coordinate and other vertices are numbered in a counter-clockwise direction. Figure 1 shows an example of such a polygon where V[1] is the first polygon. For simplicity, you can also assume all polygon vertices have distinct x and y...
Show that a set is convex if and only if its intersection with any line is...
Show that a set is convex if and only if its intersection with any line is convex. Show that a set is affine if and only if its intersection with any line is affine.
Prove using the principle of mathematical induction: (i) The number of diagonals of a convex polygon...
Prove using the principle of mathematical induction: (i) The number of diagonals of a convex polygon with n vertices is n(n − 3)/2, for n ≥ 4, (ii) 2n < n! for all n > k > 0, discover the value of k before doing induction
Suppose you want to store line segments in an R-tree. If a line segment is not...
Suppose you want to store line segments in an R-tree. If a line segment is not parallel to the axes, the bounding box for it can be large, containing a large empty area. Describe the effect on performance of having large bounding boxes on queries that ask for line segments intersecting a given region. Briefly describe a technique to improve performance for such queries and give an example of its benefit. Hint: You can divide segments into smaller pieces.
Suppose ?⃗ (?,?)=−??⃗ +??⃗ and ? is the line segment from point ?=(2,0) to ?=(0,3). (a)...
Suppose ?⃗ (?,?)=−??⃗ +??⃗ and ? is the line segment from point ?=(2,0) to ?=(0,3). (a) Find a vector parametric equation ?⃗ (?) for the line segment ? so that points ? and ? correspond to ?=0 and ?=1, respectively. ?⃗ (?)= (b) Using the parametrization in part (a), the line integral of ?⃗ along ? is ∫??⃗ ⋅??⃗ =∫???⃗ (?⃗ (?))⋅?⃗ ′(?)??=∫?? ?? with limits of integration ?= and ?= (c) Evaluate the line integral in part (b). (d)...
Evaluate the line integral, where C is the given curve. C xeyzds, Cis the line segment...
Evaluate the line integral, where C is the given curve. C xeyzds, Cis the line segment from (0, 0, 0) to (2, 3, 4)
What is the line F? What is the area A? What is the line C? What is point B? What is the area K?
What is the line F? What is the area A? What is the line C? What is point B? What is the area K?
Are there any contradictions to the law of conservation of energy in any area of science?...
Are there any contradictions to the law of conservation of energy in any area of science? State your reasons for your answer.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT