简体   繁体   English

给定边长的网格多边形面积

[英]Area of grid polygon given length of edges

Input: 输入:

Table T of length N (N <= 10000) containing 32-bit integers. 长度为N(N <= 10000)的表T包含32位整数。 Each integer denotes length and direction of consecutive edge of polygon on a square grid (every vertex has integer coordinates). 每个整数表示正方形网格上多边形连续边缘的长度和方向(每个顶点都有整数坐标)。 If integer is positive, it means edge goes up or right. 如果整数为正,则表示边沿向上或向右。 If integer is negative, edge goes down or left. 如果整数为负,则边沿向下或向左。 Edges are perpendicular to its neighbors. 边缘垂直于其邻居。 Edges do not intersect nor overlap with each other. 边缘不相交也不重叠。 Vertices do not overlap with each other. 顶点彼此不重叠。

Examplary input T: 示范性输入T:

2, -3, -1, 1, -1, 2 2,-3,-1,1,-1,2

Represents two polygons (it doesn't matter which one we will choose): 表示两个多边形(我们选择哪一个都不重要):

在此处输入图片说明

Arrows denote first edge corresponding to the first 2 in T. 箭头表示与T中的前2个相对应的第一条边。

Output: 输出:

Integer denoting surface area of given polygon. 整数,表示给定多边形的表面积。 For exemplary input, the output should be 5. 对于示例性输入,输出应为5。

Desired time complexity: O(N) 所需时间复杂度:O(N)
Desired memory complexity (not counting input): O(1) 所需的内存复杂度(不计算输入):O(1)

Can you present such algorithm and rationale behind it? 您能提出这种算法及其背后的原理吗?

Algorithm: 算法:

int area(int[] T) {
    int area = 0;
    int x = 0;
    for (int i = 0; i < T.length; i += 2) {
        x += t[i];
        area += x * t[i + 1];
    }
    return Math.abs(area);
}

Description: 描述:

Our polygon is closed in a big rectangle. 我们的多边形被封闭在一个大矩形中。 Based on this we are counting areas of small rectangles which are building our shape - rectangles which are inside polygon as well as rectangles which are outside (with opposite sign). 基于此,我们将计算正在构建形状的小矩形区域-位于多边形内部的矩形以及位于外部(带有相反符号)的矩形。

It doesn't matter from which vertex in polygon we are starting. 我们从多边形中的哪个顶点开始都没关系。 In the beginning we need one based line from which we will count vertical (it can be horizontal as well, it doesn't matter) translation of vertices. 在开始时,我们需要一条从基线开始的行,从该行我们可以计算出顶点的垂直平移(也可以是水平的,没关系)。 We are getting the first number - it will tell us, how much from the based line our first vertex is moved. 我们得到第一个数字-它会告诉我们,我们的第一个顶点从基准线移了多少。 The second number is translation according to second coordinate. 第二个数字是根据第二个坐标的平移。 By multiplying this pair we are getting the first rectangle in our sum. 通过乘以这对,我们得到了总和中的第一个矩形。

Second rectangle is described by third and fourth point: 第二个矩形由第三点和第四点描述:

  • the third number added to the first number is a vertical translation of the vertex 添加到第一个数字中的第三个数字是顶点的垂直平移

  • the fourth number is a horizontal translation of the vertex. 第四个数字是顶点的水平平移。 Multiplying this points is giving us the second rectangle. 乘以这点便得到了第二个矩形。

We are continuing to do this for the rest of numbers in table. 我们将继续针对表中的其余数字执行此操作。 We are adding areas of rectangles. 我们正在添加矩形区域。 The result is an absolute value of the sum. 结果是总和的绝对值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM