Problem: Proportional Position


Problem Statement

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.

Definition

Class

public class Proportion { public int getPoint (int left, int top, int width, int height, double percent) { // TODO: fill in code here } }

Constraints

Examples

  1. 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).

  2. 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).

  3. 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.

  4. 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.

  5. 100 100 20 20 2.2

    Returns: 144


Comments?