Consider the problem of trying to calculate a position based on its proportions relative to a given rectangle. For example, the center position is always half the width from the left and half the height from the top, but it is different depending on the rectangle you are considering: (300, 400) is the center of the rectangle with a top-left coordinate of (0, 0) and dimensions of (600, 800); or (10, 10) is the center of a rectanlge with a top-left coordinate of (5, 7) and dimensions of (10, 6).
Write the method, getPoint
, that returns the x-coordinate of the point computed based on a percentage distance from the top-left coordinate of the given rectangle.
int int int int double
int
public int getPoint (int left, int top, int width, int height, double percent)
left
and top
are non-negative numbers less than 10000
(i.e., 0 ≤ left, top < 10000
).
width
and height
are non-negative numbers less than 10000
(i.e., 0 ≤ width, height < 10000
).
percent
.
0 0 600 800 0.5
Returns: 300
The center of the rectangle starting from (0, 0) and spanning 600 by 800 pixels is (300, 400).
5 7 10 6 0.5
Returns: 10
The center of the rectangle starting from (5, 7) and spanning 10 by 6 pixels is (10, 10).
100 100 20 20 0.8
Returns: 116
The point eighty percent of the width and height inside the rectangle starting at (100, 100) and spanning 20 by 20 pixels is (116, 116). In other words, eighty percent of 20 is 16, added to 100 gives 116.
100 100 20 20 -0.2
Returns: 96
The point twenty percent of the width and height outside the rectangle starting at (100, 100) and spanning 20 by 20 pixels is (96, 96). In other words, twenty percent of 20 is 4, subtracted from 100 gives 96.
100 100 20 20 2.2
Returns: 144