top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Find the area of the Orthogonal Polygon.

+5 votes
645 views

Given an orthogonal polygon, calculate the area, and print it on screen. You will be given input as pairs of direction and length. Input will be provided in the form, direction indicator NEWS, followed by the distance indicator 1-99.

Sample 
Input => N5 E5 S5 W5. 
Output => 25 
Reason => This is a square of side 5, area is 25. 
Input => N10 E2 S8 E8 S2 W10 
Output => 36 
Reason => Depicts L shape. 
          This can be computed as Vertical Rectangle = 2x8, 
          Horizontal Rectangle = 2x8 and common square = 2x2.
posted Dec 15, 2013 by Atiqur Rahman

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
You have given part of solution by yourself, since it is orthogonal hence all edges will be at orthogonal of each other. So we we divide the whole polygon into smaller rectangles (lets assume every square is an rectangle) and calculate the area separately and add.

So now the problem is to divide into rectangles.

1 Answer

+2 votes
 
Best answer

Over and above Satish comment and help from Quora -

  1. Segment N10, moving from (0, 0) to (0, 10). This increases y from 0 to 10, but because x is unchanged, the change in area is zero, because (Δx * y) = (0 * any number between 0 and 10) = 0.
  2. Segment E2, moving from (0, 10) to (2, 10). The area is increased by (Δx * y) = (2 * 10) = 20.
  3. Segment S8, moving from (2, 10) to (2, 2). This decreases y but does not change x. The area does not change (remains at 20).
  4. Segment E8, moving from (2, 2) to (10, 2). The area is increased by (Δx * y) = (8 * 2) = 16. The total area is now (20 + 16) = 36.
  5. Segment S2, moving from (10, 2) to (10, 0). This decreases y but does not change x. The total area does not change (remains at 36).
  6. Segment W10, moving from (10, 0) to (0, 0). The area is changed by (Δx * y) = (-10 * 0), which happens to be zero.

    The total is, thus, (0 + 20 + 0 + 16 + 0 + 0) = 36.

answer Dec 15, 2013 by Salil Agrawal
good solution.... sir but i am not sure for all edge cases...if so..i will add those after checking them.
Did not get you, what do you mean by all edges cases...
Similar Questions
+6 votes

I was trying to get maximum rectangle area for a given histogram but I used brute force approach which have O(n^2) time complexity so I want some better solution using stack so that we could reduce time complexity to O(n) or O(log n ).

+7 votes

A binary tree is given and two nodes of this tree is given, we have to find out the algorithm/code for lowest common ancestor of the two given nodes. Common ancestor are the nodes which can be reached from two nodes by following the parent links. Lowest common ancestor is the common ancestor which has the highest depth.

+5 votes

Given an unsorted array, find the max length of subsequence in which the numbers are in incremental order.

For example: If the input array is {7, 2, 3, 1, 5, 8, 9, 6}, a subsequence with the most numbers in incremental order is {2, 3, 5, 8, 9} and the expected output is 5.

+3 votes
               40

       30              60

   25      35      50      70

 12  27  32

Answer = -59

...