Shoelace Formula

Joined
Nov 22, 2023
Messages
16
Reaction score
0
have anyone can give some code of Shoelace Formula program ?

task:
Read a set of Pi(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5) parameters representing some polygons from a text file,
e.g. polygonVertices.txt.

-44 -42 -50 -53 -28 -51 55 -37 99 21


58 -86 62 -55 100 60 70 75 -19 72


Write a Java method to calculate the area of ten polygons by passing their
parameters as inputs. (15 marks)

Display the polygons vertices as shown in part (A) in Appendix A. (together with their number) (15 marks)

Display the smallest and the biggest area (together with their number)
as shown in part (B) in Appendix A. (30 marks)

Display the 5 biggest polygon (together with their number, area and vertices) as shown
in part (C) in Appendix A.

deadline: 28 November
 
Joined
Nov 22, 2023
Messages
16
Reaction score
0
sample output

(A) Show inputs for one polygon each line

Input Polygons:
1 (-44,-42)(-50,-53)(-28,-51)(55,-37)(99,21)
2 (-18,-26)(57,-48)(78,-19)(24,-11)(-56,29)
3 (-98,0)(-65,-8)(88,-74)(79,98)(-21,62)
4 (-95,-95)(-29,-21)(29,21)(-44,52)(-56,1)
5 (-93,-33)(2,-22)(74,-100)(53,-16)(20,95)
6 (-82,-13)(-80,-79)(93,-13)(58,17)(-35,51)


(B) Show the smallest and the biggest polygon
7 (94,-35)(64,26)(-17,46)(-37,56)(-65,61)
8 (-39,-21)(7,-98)(18,-10)(5,44)(-80,68)
9 (-63,7)(-76,-35)(65,31)(1,91)(-30,48)
10 (58,-86)(62,-55)(100,60)(70,75)(-19,72)

The smallest polygon is No.2 . The area is 3331.5 .
The biggest polygon is No.3 . The area is 16900.0 .

The top 5 biggest polygons:
Rank No. Area Vertices


(C) Show the 5 biggest polygons
1 3 16900.0 (-98,0)(-65,-8)(88,-74)(79,98)(-21,62)
2 6 12175.0 (-82,-13)(-80,-79)(93,-13)(58,17)(-35,51)
3 5 10593.0 (-93,-33)(2,-22)(74,-100)(53,-16)(20,95)
4 10 9292.5 (58,-86)(62,-55)(100,60)(70,75)(-19,72)
5 8 7348.5 (-39,-21)(7,-98)(18,-10)(5,44)(-80,68)
 
Joined
Nov 22, 2023
Messages
16
Reaction score
0
Please show us what you have done
only the templete
import java.io.*;
import java.util.*;

public class Sample {

final static int MAX_NO_POLYGONS = 10; // maximum number of input polygons

public static void main( String[] args ) throws Exception {

int p[][] = new int[MAX_NO_POLYGONS][10]; // array to store polygons parameters
int nPolygons; // number of polygons read

nPolygons = readPolygonsFile( "polygonVertices.txt", p );

System.out.println( "Polygons read from polygonVertices.txt" );
for (int i=0; i<nPolygons; i++)
for (int j=0; j<10; j++)
System.out.println( p[j] + ",");
}

/***************************************************************************
This is a method to read the parameters (x1, y1, x2, .... y4, x5, y5)
of polygon from a text file.
Inputs:
filename is the file name of the input file
Outputs:
polygons[][] is a 2-dimensional array to store parameters
e.g. polygons[0][0] stores the x1-coordinate of the 1st polygon
polygons[0][1] stores the y1-coordinate of the 1st polygon
...
polygons[0][8] stores the x5-coordinate of the 1st polygon
polygons[0][9] stores the y5-coordinate of the 1st polygon
...
polygons[n-1][0] stores the x1-coordinate of the last polygon
polygons[n-1][1] stores the y1-coordinate of the last polygon
...
polygons[n-1][8] stores the x5-coordinate of the last polygon
polygons[n-1][9] stores the y5-coordinate of the last polygon
Return value:
the total number of polygons read
***************************************************************************/
public static int readPolygonsFile( String filename, int[][] polygons ) throws Exception {

int lineNumber = 0;

// Create a File Reader instance and read parameters of polygon from the input file
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
String line;

// Get all parameters from each line (One Polygon)
while ((line = br.readLine()) != null) {
String[] numbers = line.split(" ");
List<Integer> integers = new ArrayList<>();

// Insert all coordinate into Integer Array for each polygon
for(int i = 0; i<numbers.length; i++) {
polygons[lineNumber] = Integer.parseInt(numbers);
}

lineNumber++;
}
} catch (IOException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
System.out.println("Error parsing a number.");
e.printStackTrace();
}

return lineNumber;
}

}
 
Joined
Sep 21, 2022
Messages
186
Reaction score
26
I didn't know about this formula, it might be an interesting read. Thanks.

If you want to cheat on your assignment, the web site called Rosetta Code has a Java version of the shoelace formula.

It may have bugs, but that's the risk you take.
 
Joined
Nov 22, 2023
Messages
16
Reaction score
0
the output is this
import java.util.List;

public class ShoelaceFormula {
private static class Point {
int x, y;

Point(int x, int y) {
this.x = x;
this.y = y;
}

@Override
public String toString() {
return String.format("(%d, %d)", x, y);
}
}

private static double shoelaceArea(List<Point> v) {
int n = v.size();
double a = 0.0;
for (int i = 0; i < n - 1; i++) {
a += v.get(i).x * v.get(i + 1).y - v.get(i + 1).x * v.get(i).y;
}
return Math.abs(a + v.get(n - 1).x * v.get(0).y - v.get(0).x * v.get(n - 1).y) / 2.0;
}

public static void main(String[] args) {
List<Point> v = List.of(
new Point(3, 4),
new Point(5, 11),
new Point(12, 8),
new Point(9, 5),
new Point(5, 6)
);
double area = shoelaceArea(v);
System.out.printf("Given a polygon with vertices %s,%n", v);
System.out.printf("its area is %f,%n", area);
}
}
Output:
Given a polygon with vertices [(3, 4), (5, 11), (12, 8), (9, 5), (5, 6)],
its area is 30.000000,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,039
Messages
2,570,376
Members
47,032
Latest member
OdellBerg4

Latest Threads

Top