34 Replies
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Tomasm21
Tomasm213y ago
public class CalculateArea {

public static void main(String[] args) {
// bi-dimensional array: coord[4][2];
float[][] coord = {{0f, 0f}, {2f, 0f}, {2f, 2f}, {0f, 2f}};

float area = calculateArea(coord);

showArea(area);
}

public static float calculateArea (float[][] coord) {
float area = 0;
//(x0+x1)(y1-y0) + (x1+x2)(y2-y1) + (x2+x3)(y3-y2) + (x3+x0)(y0-y3);
area = ((coord[0][0]+coord[1][0])*(coord[1][1]-coord[0][1]) +
(coord[1][0]+coord[2][0])*(coord[2][1]-coord[1][1]) +
(coord[2][0]+coord[3][0])*(coord[3][1]-coord[2][1]) +
(coord[3][0]+coord[0][0])*(coord[0][1]-coord[3][1])
)/2;
return area;
}

public static void showArea (float area) {
System.out.print("The area is: "+area);
}
}
public class CalculateArea {

public static void main(String[] args) {
// bi-dimensional array: coord[4][2];
float[][] coord = {{0f, 0f}, {2f, 0f}, {2f, 2f}, {0f, 2f}};

float area = calculateArea(coord);

showArea(area);
}

public static float calculateArea (float[][] coord) {
float area = 0;
//(x0+x1)(y1-y0) + (x1+x2)(y2-y1) + (x2+x3)(y3-y2) + (x3+x0)(y0-y3);
area = ((coord[0][0]+coord[1][0])*(coord[1][1]-coord[0][1]) +
(coord[1][0]+coord[2][0])*(coord[2][1]-coord[1][1]) +
(coord[2][0]+coord[3][0])*(coord[3][1]-coord[2][1]) +
(coord[3][0]+coord[0][0])*(coord[0][1]-coord[3][1])
)/2;
return area;
}

public static void showArea (float area) {
System.out.print("The area is: "+area);
}
}
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Tomasm21
Tomasm213y ago
To calculate not only square, but any polygon area, the function is:
public static float calculateAnyPolygonArea (float[][] coord) {
int side_num = coord.length;
float area = 0;
int k = 0;

for(int i = 0; i < side_num; i++) {
k = i+1;
if(k >= side_num) k=0;
area = area + (coord[i][0]+coord[k][0])*(coord[k][1]-coord[i][1]);
}
return area/2;
}
public static float calculateAnyPolygonArea (float[][] coord) {
int side_num = coord.length;
float area = 0;
int k = 0;

for(int i = 0; i < side_num; i++) {
k = i+1;
if(k >= side_num) k=0;
area = area + (coord[i][0]+coord[k][0])*(coord[k][1]-coord[i][1]);
}
return area/2;
}
It works only if you assign adjacent points coordinates into the array one after the other consistently. Do not mix points coordinates chaotically.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Tomasm21
Tomasm213y ago
You're welcome
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Tomasm21
Tomasm213y ago
instead of 0 and 2? or why 0 and 2? f because type is float
Tomasm21
Tomasm213y ago
www.javatpoint.com
Java Float Keyword - Javatpoint
Java Float Keyword with java keywords, java tutorial, features, history, variables, object, class, programs, operators, oops concept, inheritance, array, string, methods, examples etc.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Tomasm21
Tomasm213y ago
just an example you can write any numbers you like but be sure that in the end it will be polygon
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Tomasm21
Tomasm213y ago
yes area will be 1 if 4 points square but you can use any polygon coordinates.
Tomasm21
Tomasm213y ago
for example from this picture:
No description
Tomasm21
Tomasm213y ago
float[][] coord = {{2f, 3f}, {4f, -4f}, {-3f, -2f}, {-4f, 4f}}; in this example the area will be -40.0 minus but this could be fixed by: if (area < 0) area = -area;
public static float calculateAnyPolygonArea (float[][] coord) {
int side_num = coord.length;
float area = 0;
int k = 0;

for(int i = 0; i < side_num; i++) {
k = i+1;
if(k >= side_num) k=0;
area = area + (coord[i][0]+coord[k][0])*(coord[k][1]-coord[i][1]);
}
if (area < 0) area = -area;
return area/2;
}
public static float calculateAnyPolygonArea (float[][] coord) {
int side_num = coord.length;
float area = 0;
int k = 0;

for(int i = 0; i < side_num; i++) {
k = i+1;
if(k >= side_num) k=0;
area = area + (coord[i][0]+coord[k][0])*(coord[k][1]-coord[i][1]);
}
if (area < 0) area = -area;
return area/2;
}
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Tomasm21
Tomasm213y ago
@YavuzSultanEnes An example from website: https://www.mathopenref.com/coordpolygonarea.html float[][] coord = {{4f, 10f}, {9f, 7f}, {11f, 2f}, {2f, 2f}}; area will be 45.5
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Tomasm21
Tomasm213y ago
yes
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Tomasm21
Tomasm213y ago
from our example about square yes x would be rows y columns
Tomasm21
Tomasm213y ago
Here is another example:
No description
Tomasm21
Tomasm213y ago
float[][] coord = {{3f, 3f}, {4f, 2f}, {3f, 1f}, {2f, -1f}, {1f, 1f}, {-1f, 2f}}; area will be 8.0 On the Internet there is another solution similar, but a bit fifferent:
Tomasm21
Tomasm213y ago
GeeksforGeeks
Area of a polygon with given n ordered vertices - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Tomasm21
Tomasm213y ago
with two arrays but not bidimensional array when we say point 2 out of 4 coord 2 it's index is 1 because if you have 4 length array, it's indexes are 0, 1, 2, 3
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Tomasm21
Tomasm213y ago
1st is 0, 2nd - 1, 3rd - 2, 4th - 3 3 is Fourth point 4th vertex or point
Tomasm21
Tomasm213y ago
@YavuzSultanEnes
No description
Tomasm21
Tomasm213y ago
coord[n][0] - X coordinate, coord[n][1] - Y coordinate 0 0 2 0 2 2 0 2 square matrix float[][] coord = {{0f, 0f}, {2f, 0f}, {2f, 2f}, {0f, 2f}};
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Tomasm21
Tomasm213y ago
A square as a polygon has 4 vertices. Each vertex has two coordinates - X coordinate and Y coordinate.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
JavaBot
JavaBot3y ago
Post Closed
This post has been closed by <@257204197013979138>.

Did you find this page helpful?