Determining the Size of a Rectangle based on Co-ordinates in Java -
i've been struggling problem awhile , i'm looking assistance.
information know/am given:
- a series of co-ordinates similar following
- x=5, y=5
- x=5, y=6
- x=9, y=10
- x=1, y=1
- x=2, y=1
- x=1, y=2
- x=2, y=2
- every point in list part of rectangle, @ least 1x1
- these lists
int[]
s, x-coords in 1 array, , y-coords in another - the array co-ordinates matched (e.g.
xcoords[5]
makes ordered pairycoords[5]
) - the size of arrays not consistent (it might 50 x-coords , 50 y-coords)
- the co-ordinates can negative
- a series of co-ordinates similar following
what need find
- the number of rectangles in list (e.g. example 3)
- the size of each rectangle (e.g. example r1=2x1, r2=1x1, r3=2x2)
- the format of results don't matter
example:
public class rectangle_test { public static void main(string[] args) { int[] xcoords = {5,5,9,1,2,1,2}; int[] ycoords = {5,6,10,1,1,2,2}; } }
i have idea of how approach problem...
- get first element of each array , assign variable
int initialx = xcoords[0];
int initialy = ycoords[0];
2. loop through arrays, , compare initial values each value in array, , reset if both different
if(initialx != xcoords[currentindex] && initialy != ycoords[currentindex] && xcoords[currentindex] != xcoords[currentindex+1] && ycoords[currentindex] != ycoords[currentindex+1]){ initialx = xcoords[currentindex]; initialy = ycoords[currentindex]; numrects++; }
- however, can't figure out how determine rectangle size code above...
Comments
Post a Comment