示例#1
0
def find_centroid(polygon):
    """Find the centroid of a polygon. If a polygon has 0 area, use the latitude
    and longitude of its first position as its centroid.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    Arguments:
    polygon -- A list of positions, in which the first and last are the same

    Returns 3 numbers: centroid latitude, centroid longitude, and polygon area.

    >>> p1 = make_position(1, 2)
    >>> p2 = make_position(3, 4)
    >>> p3 = make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1] # First vertex is also the last vertex
    >>> round_all = lambda s: [round(x, 5) for x in s]
    >>> round_all(find_centroid(triangle))
    [3.0, 2.0, 6.0]
    >>> round_all(find_centroid([p1, p3, p2, p1])) # reversed
    [3.0, 2.0, 6.0]
    >>> apply_to_all(float, find_centroid([p1, p2, p1])) # A zero-area polygon
    [1.0, 2.0, 0.0]
    """
    total_area, area, i, c_lat, c_lon = 0, 0, 0, 0, 0
    while i < len(polygon) - 1:
        area = latitude(polygon[i])*longitude(polygon[i+1]) - latitude(polygon[i+1])*longitude(polygon[i])
        c_lat += area * (latitude(polygon[i]) + latitude(polygon[i+1]))
        c_lon += area * (longitude(polygon[i]) + longitude(polygon[i+1]))
        total_area += area
        i += 1
    total_area = total_area / 2
    if total_area == 0:
        return [latitude(polygon[0]), longitude(polygon[0]), 0]
    return [c_lat/(6*total_area), c_lon/(6*total_area), abs(total_area)]
示例#2
0
文件: trends.py 项目: keyu-lai/cs61a
def find_centroid(polygon):
    """Find the centroid of a polygon. If a polygon has 0 area, use the latitude
    and longitude of its first position as its centroid.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    Arguments:
    polygon -- A list of positions, in which the first and last are the same

    Returns 3 numbers: centroid latitude, centroid longitude, and polygon area.

    >>> p1 = make_position(1, 2)
    >>> p2 = make_position(3, 4)
    >>> p3 = make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1] # First vertex is also the last vertex
    >>> round_all = lambda s: [round(x, 5) for x in s]
    >>> round_all(find_centroid(triangle))
    [3.0, 2.0, 6.0]
    >>> round_all(find_centroid([p1, p3, p2, p1])) # reversed
    [3.0, 2.0, 6.0]
    >>> apply_to_all(float, find_centroid([p1, p2, p1])) # A zero-area polygon
    [1.0, 2.0, 0.0]
    """
    "*** YOUR CODE HERE ***"
    def helper(xi, yi, xi1, yi1):
        term = xi*yi1-xi1*yi
        return Cx+(xi+xi1)*term, Cy+(yi+yi1)*term, A+0.5*term
    Cx, Cy, A = 0, 0, 0
    for c in range(len(polygon)-1):
        Cx, Cy, A = helper(latitude(polygon[c]), longitude(polygon[c]), latitude(polygon[c+1]), longitude(polygon[c+1]))
    return [Cx/(6*A), Cy/(6*A), abs(A)] if A else [latitude(polygon[0]), longitude(polygon[0]), 0.0]
示例#3
0
def find_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    "*** YOUR CODE HERE ***"
    # Case 1: State is only a single polygon
    if len(polygons) == 1:
        return make_position(latitude(find_centroid(polygons[0])), longitude(find_centroid(polygons[0])))
    # Case 2: States are multiple polygons
    x_weight = 0
    y_weight = 0
    area = 0
    for poly in polygons: # Using the formula from Wikipedia, we calculate the sum of the X and Y coordinates per polygon weighting it by the area.
        x_weight += latitude(find_centroid(poly)) * find_centroid(poly)[2]
        y_weight += longitude(find_centroid(poly)) * find_centroid(poly)[2]
        area += find_centroid(poly)[2]
    return make_position(x_weight/area, y_weight/area)
示例#4
0
 def poly_area(x=0):
     area = 0
     while x < len(polygon) - 1:
         area+=(0.5)*(latitude(polygon[x])*longitude(polygon[x+1])-\
                (latitude(polygon[x+1])*longitude(polygon[x])))
         x += 1
     return area
示例#5
0
文件: trends.py 项目: davjav/trends
def find_centroid(polygon):
    """Find the centroid of a polygon. If a polygon has 0 area, use the latitude
    and longitude of its first position as its centroid.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    Arguments:
    polygon -- A list of positions, in which the first and last are the same

    Returns 3 numbers: centroid latitude, centroid longitude, and polygon area.

    >>> p1 = make_position(1, 2)
    >>> p2 = make_position(3, 4)
    >>> p3 = make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1] # First vertex is also the last vertex
    >>> round_all = lambda s: [round(x, 5) for x in s]
    >>> round_all(find_centroid(triangle))
    [3.0, 2.0, 6.0]
    >>> round_all(find_centroid([p1, p3, p2, p1])) # reversed
    [3.0, 2.0, 6.0]
    >>> apply_to_all(float, find_centroid([p1, p2, p1])) # A zero-area polygon
    [1.0, 2.0, 0.0]
    """
    cent_lat, cent_long, poly_area = 0, 0, 0
    for vertex in list(range(len(polygon) - 1)):
        poly_area += .5 * (latitude(polygon[vertex]) * longitude(polygon[vertex + 1]) - latitude(polygon[vertex + 1]) * longitude(polygon[vertex]))
    if poly_area == 0:
        return latitude(polygon[0]), longitude(polygon[0]), 0
    for vertex in list(range(len(polygon) - 1)):
        cent_lat += (1 / (6 * poly_area)) * (latitude(polygon[vertex]) + latitude(polygon[vertex + 1])) * (latitude(polygon[vertex]) * longitude(polygon[vertex + 1]) - latitude(polygon[vertex + 1]) * longitude(polygon[vertex]))
        cent_long += (1 / (6 * poly_area)) * (longitude(polygon[vertex]) + longitude(polygon[vertex + 1])) * (latitude(polygon[vertex]) * longitude(polygon[vertex + 1]) - latitude(polygon[vertex + 1]) * longitude(polygon[vertex]))
    return cent_lat, cent_long, abs(poly_area)
示例#6
0
 def cent_lat(polygon):
     lat_value = 0
     for _ in range(len(polygon) - 1):
         lat_value += (latitude(polygon[_]) + latitude(polygon[_ + 1])) * (
             (latitude(polygon[_ + 1]) * longitude(polygon[_])) -
             (latitude(polygon[_]) * longitude(polygon[_ + 1])))
     return (1 / (6 * area_poly)) * lat_value
示例#7
0
文件: trends.py 项目: mgpanpan/CS61A
 def poly_area(x=0):
     area = 0
     while x < len(polygon)-1:
         area+=(0.5)*(latitude(polygon[x])*longitude(polygon[x+1])-\
                (latitude(polygon[x+1])*longitude(polygon[x])))
         x+=1
     return area
示例#8
0
def find_centroid(polygon):
    """Find the centroid of a polygon.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    polygon -- A list of positions, in which the first and last are the same

    Returns: 3 numbers; centroid latitude, centroid longitude, and polygon area

    Hint: If a polygon has 0 area, use the latitude and longitude of its first
    position as its centroid.

    >>> p1, p2, p3 = make_position(1, 2), make_position(3, 4), make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1]  # First vertex is also the last vertex
    >>> round5 = lambda x: round(x, 5) # Rounds floats to 5 digits
    >>> tuple(map(round5, find_centroid(triangle)))
    (3.0, 2.0, 6.0)
    >>> tuple(map(round5, find_centroid([p1, p3, p2, p1])))
    (3.0, 2.0, 6.0)
    >>> tuple(map(float, find_centroid([p1, p2, p1])))  # A zero-area polygon
    (1.0, 2.0, 0.0)
    """
    "*** YOUR CODE HERE ***"
    
    
    """
    def area(polygon):
        area_value = 0
        for _ in range(len(polygon) - 1):
            area_value += (latitude(polygon[_]) * longitude(polygon[_ + 1])) - (latitude(polygon[_ + 1]) * longitude(polygon[_]))
        return .5 * area_value
    area_poly = area(polygon)
    if area(polygon) == 0:
        return latitude(polygon[0]), longitude(polygon[0]), area(polygon)
    def cent_lat(polygon):
        lat_value = 0
        for _ in range(len(polygon) - 1):
            lat_value += (latitude(polygon[_]) + latitude(polygon[_ + 1])) * ((latitude(polygon[_ + 1]) * longitude(polygon[_])) - (latitude(polygon[_]) * longitude(polygon[_ + 1])))
        return (1/(6 * area_poly)) * lat_value
    def cent_lon(polygon):
        lon_value = 0
        for _ in range(len(polygon) - 1):
            lon_value += (longitude(polygon[_]) + longitude(polygon[_ + 1])) * ((latitude(polygon[_ + 1]) * longitude(polygon[_])) - (latitude(polygon[_]) * longitude(polygon[_ + 1])))
        return (1/(6 * area_poly)) * lon_value
    
    
    return -1 * cent_lat(polygon), -1 * cent_lon(polygon), abs(area_poly)"""
    
    area_value, lat_value, lon_value = 0, 0, 0
    for _ in range(len(polygon) - 1):
        lat1, lat2, lon1, lon2 = latitude(polygon[_]), latitude(polygon[_ + 1]), longitude(polygon[_]), longitude(polygon[_ + 1])
        area_value += lat1 * lon2 - lat2 * lon1
        lat_value += (lat1 + lat2) * (lat1 * lon2 - lat2 * lon1)
        lon_value += (lon1 + lon2) * (lat1 * lon2 - lat2 * lon1)
    area_poly = .5 * area_value
    if area_poly == 0:
        return latitude(polygon[0]), longitude(polygon[0]), area_poly
    lat_poly = (1/(6 * area_poly)) * lat_value
    lon_poly = (1/(6 * area_poly)) * lon_value
    return lat_poly, lon_poly, abs(area_poly)
示例#9
0
文件: trends.py 项目: icring/CS-61A
def find_centroid(polygon):
    """Find the centroid of a polygon. If a polygon has 0 area, use the latitude
    and longitude of its first position as its centroid.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    Arguments:
    polygon -- A list of positions, in which the first and last are the same

    Returns 3 numbers: centroid latitude, centroid longitude, and polygon area.

    >>> p1 = make_position(1, 2)
    >>> p2 = make_position(3, 4)
    >>> p3 = make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1] # First vertex is also the last vertex
    >>> round_all = lambda s: [round(x, 5) for x in s]
    >>> round_all(find_centroid(triangle))
    [3.0, 2.0, 6.0]
    >>> round_all(find_centroid([p1, p3, p2, p1])) # reversed
    [3.0, 2.0, 6.0]
    >>> apply_to_all(float, find_centroid([p1, p2, p1])) # A zero-area polygon
    [1.0, 2.0, 0.0]
    """
    "*** YOUR CODE HERE ***"
    cx, cy, area, index = 0, 0, 0, 0
    while index < len(polygon) - 1:
        x, y, x_next, y_next = latitude(polygon[index]), longitude(polygon[index]), latitude(polygon[index+1]), longitude(polygon[index+1])
        temparea = x * y_next - x_next * y
        cx, cy, area, index = cx + (x + x_next) * temparea, cy + (y + y_next) * temparea, area + temparea, index + 1
    area = area * 0.5
    return [latitude(polygon[0]), longitude(polygon[0]), 0] if area == 0 else [(1/(6*area))*cx, (1/(6*area))*cy, abs(area)]
示例#10
0
def find_centroid(polygon):
	"""Find the centroid of a polygon.

	http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

	polygon -- A list of positions, in which the first and last are the same

	Returns: 3 numbers; centroid latitude, centroid longitude, and polygon area

	Hint: If a polygon has 0 area, use the latitude and longitude of its first
	position as its centroid.

	>>> p1, p2, p3 = make_position(1, 2), make_position(3, 4), make_position(5, 0)
	>>> triangle = [p1, p2, p3, p1]  # First vertex is also the last vertex
	>>> round5 = lambda x: round(x, 5) # Rounds floats to 5 digits
	>>> tuple(map(round5, find_centroid(triangle)))
	(3.0, 2.0, 6.0)
	>>> tuple(map(round5, find_centroid([p1, p3, p2, p1])))
	(3.0, 2.0, 6.0)
	>>> tuple(map(float, find_centroid([p1, p2, p1])))  # A zero-area polygon
	(1.0, 2.0, 0.0)
	"""
	area, cent_lat, cent_lon = 0, 0, 0
	for i in range(len(polygon) - 1):
		x1, y1 = latitude(polygon[i]), longitude(polygon[i])
		x2, y2 = latitude(polygon[i+1]), longitude(polygon[i+1])
		area += .5 * (x1*y2 - x2*y1)
		cent_lat += (x1+x2) * (x1*y2 - x2*y1)
		cent_lon += (y1+y2) * (x1*y2 - x2*y1)
	if area == 0:
		return latitude(polygon[0]), longitude(polygon[0]), area
	return cent_lat / (6*area), cent_lon / (6*area), abs(area)
示例#11
0
def find_centroid(polygon):
    """Find the centroid of a polygon.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    polygon -- A list of positions, in which the first and last are the same

    Returns: 3 numbers; centroid latitude, centroid longitude, and polygon area

    Hint: If a polygon has 0 area, return its first position as its centroid

    >>> p1, p2, p3 = make_position(1, 2), make_position(3, 4), make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1]  # First vertex is also the last vertex
    >>> find_centroid(triangle)
    (3.0, 2.0, 6.0)
    >>> find_centroid([p1, p3, p2, p1])
    (3.0, 2.0, 6.0)
    >>> tuple(map(float, find_centroid([p1, p2, p1]))) # Forces result to be floats
    (1.0, 2.0, 0.0)
    """
    area, x_point, y_point = 0, 0, 0
    for k in range(0, len(polygon) - 1):
        general_formula = (latitude(polygon[k])*longitude(polygon[k+1])-latitude(polygon[k+1])*longitude(polygon[k]))
        x_point += (latitude(polygon[k])+latitude(polygon[k+1]))*general_formula
        y_point += (longitude(polygon[k])+longitude(polygon[k+1]))*general_formula
        area += general_formula
    if area != 0:
        area = area/2
        x_point = (x_point/(6*area))
        y_point = (y_point/(6*area))
        area = abs(area)
    else:
        x_point, y_point = (latitude(polygon[0]), longitude(polygon[0]))
    return (x_point, y_point, area)
示例#12
0
 def area(polygon):
     area_value = 0
     for _ in range(len(polygon) - 1):
         area_value += (latitude(polygon[_]) * longitude(
             polygon[_ + 1])) - (latitude(polygon[_ + 1]) *
                                 longitude(polygon[_]))
     return .5 * area_value
示例#13
0
def find_centroid(polygon):
    """Find the centroid of a polygon.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    polygon -- A list of positions, in which the first and last are the same

    Returns: 3 numbers; centroid latitude, centroid longitude, and polygon area

    Hint: If a polygon has 0 area, use the latitude and longitude of its first
    position as its centroid.

    >>> p1, p2, p3 = make_position(1, 2), make_position(3, 4), make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1]  # First vertex is also the last vertex
    >>> round5 = lambda x: round(x, 5) # Rounds floats to 5 digits
    >>> tuple(map(round5, find_centroid(triangle)))
    (3.0, 2.0, 6.0)
    >>> tuple(map(round5, find_centroid([p1, p3, p2, p1])))
    (3.0, 2.0, 6.0)
    >>> tuple(map(float, find_centroid([p1, p2, p1])))  # A zero-area polygon
    (1.0, 2.0, 0.0)
    """
    "*** YOUR CODE HERE ***"
    """
    def area(polygon):
        area_value = 0
        for _ in range(len(polygon) - 1):
            area_value += (latitude(polygon[_]) * longitude(polygon[_ + 1])) - (latitude(polygon[_ + 1]) * longitude(polygon[_]))
        return .5 * area_value
    area_poly = area(polygon)
    if area(polygon) == 0:
        return latitude(polygon[0]), longitude(polygon[0]), area(polygon)
    def cent_lat(polygon):
        lat_value = 0
        for _ in range(len(polygon) - 1):
            lat_value += (latitude(polygon[_]) + latitude(polygon[_ + 1])) * ((latitude(polygon[_ + 1]) * longitude(polygon[_])) - (latitude(polygon[_]) * longitude(polygon[_ + 1])))
        return (1/(6 * area_poly)) * lat_value
    def cent_lon(polygon):
        lon_value = 0
        for _ in range(len(polygon) - 1):
            lon_value += (longitude(polygon[_]) + longitude(polygon[_ + 1])) * ((latitude(polygon[_ + 1]) * longitude(polygon[_])) - (latitude(polygon[_]) * longitude(polygon[_ + 1])))
        return (1/(6 * area_poly)) * lon_value
    
    
    return -1 * cent_lat(polygon), -1 * cent_lon(polygon), abs(area_poly)"""

    area_value, lat_value, lon_value = 0, 0, 0
    for _ in range(len(polygon) - 1):
        lat1, lat2, lon1, lon2 = latitude(polygon[_]), latitude(
            polygon[_ + 1]), longitude(polygon[_]), longitude(polygon[_ + 1])
        area_value += lat1 * lon2 - lat2 * lon1
        lat_value += (lat1 + lat2) * (lat1 * lon2 - lat2 * lon1)
        lon_value += (lon1 + lon2) * (lat1 * lon2 - lat2 * lon1)
    area_poly = .5 * area_value
    if area_poly == 0:
        return latitude(polygon[0]), longitude(polygon[0]), area_poly
    lat_poly = (1 / (6 * area_poly)) * lat_value
    lon_poly = (1 / (6 * area_poly)) * lon_value
    return lat_poly, lon_poly, abs(area_poly)
示例#14
0
文件: trends.py 项目: mgpanpan/CS61A
 def cent_longitude(x = 0):
     cent_long = 0
     while x < len(polygon)-1:
         cent_long += (longitude(polygon[x])+longitude(polygon[x+1]))*\
                      (latitude(polygon[x])*longitude(polygon[x+1])-\
                       latitude(polygon[x+1])*longitude(polygon[x]))
         x+=1
     return (cent_long)/(6*new_area)
示例#15
0
 def cent_longitude(x=0):
     cent_long = 0
     while x < len(polygon) - 1:
         cent_long += (longitude(polygon[x])+longitude(polygon[x+1]))*\
                      (latitude(polygon[x])*longitude(polygon[x+1])-\
                       latitude(polygon[x+1])*longitude(polygon[x]))
         x += 1
     return (cent_long) / (6 * new_area)
示例#16
0
 def vert_center(n):
     if area(len(polygon) - 1) == 0:
         return longitude(polygon[0])
     elif n == 0:
         return 0
     else:
         x0, x1 = latitude(polygon[n - 1]), latitude(polygon[n])
         y0, y1 = longitude(polygon[n - 1]), longitude(polygon[n])
         return (1 / (6 * area(len(polygon) - 1))) * (y0 + y1) * (x0 * y1 - x1 * y0) + vert_center(n - 1)
示例#17
0
 def Area(polygon):
     l = len(polygon)-1
     temp = 0
     area =0
     while(temp<l):
         area+= (latitude(polygon[temp]) *longitude(polygon[temp+1])-latitude(polygon[temp+1])*longitude(polygon[temp]))
         temp +=1
     area = area/2
     return (area)
示例#18
0
 def area(n):
     if (len(polygon) - 1) <= 2:
         return 0
     elif n == 0:
         return 0
     else:
         x0, x1 = latitude(polygon[n - 1]), latitude(polygon[n])
         y0, y1 = longitude(polygon[n - 1]), longitude(polygon[n])
         return (1 / 2 * (x0 * y1 - x1 * y0)) + area(n - 1)
示例#19
0
 def centroid_helper(fn):
     value = 0
     i = 0
     while i < len(polygon) - 1:
         value = value + (fn(polygon[i]) + fn(polygon[i + 1])) * (
             (latitude(polygon[i]) * longitude(polygon[i + 1])) -
             (latitude(polygon[i + 1]) * longitude(polygon[i])))
         i += 1
     return value / (6 * area)
示例#20
0
 def compute_center_y(polygon):
     sum = 0
     for i in range(num_sides):
         expression1 = (longitude(polygon[i]) + longitude(polygon[i + 1]))
         expression2 = (latitude(polygon[i]) * longitude(polygon[i + 1]) -
                        latitude(polygon[i + 1]) * longitude(polygon[i]))
         sum += expression1 * expression2
     if area == 0:
         return longitude(polygon[0])
     return sum / (6 * area)
示例#21
0
def find_centroid(polygon):
    """Find the centroid of a polygon.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    polygon -- A list of positions, in which the first and last are the same

    Returns: 3 numbers; centroid latitude, centroid longitude, and polygon area

    Hint: If a polygon has 0 area, return its first position as its centroid

    >>> p1, p2, p3 = make_position(1, 2), make_position(3, 4), make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1]  # First vertex is also the last vertex
    >>> find_centroid(triangle)
    (3.0, 2.0, 6.0)
    >>> find_centroid([p1, p3, p2, p1])
    (3.0, 2.0, 6.0)
    >>> find_centroid([p1, p2, p1])
    (1, 2, 0)
    """
    "*** YOUR CODE HERE ***"
    #we will talk about this in class, but it is pretty straightforward.
    Cx=0
    Cy=0
    A =0

    count = len(polygon)

    for i in range(count-1):
        point1 = polygon[i]
        point2 = polygon[i+1]

        x1 = latitude(point1)
        x2 = latitude(point2)
        y1 = longitude (point1)
        y2 = longitude (point2)

        foo = x1 * y2
        bar = x2 * y1
        result = foo - bar

        A += result
        Cx += (x1+x2)*(x1*y2-x2*y1)
        Cy += (y1+y2)*(x1*y2-x2*y1)
    A = (A/2.0)

    if A == 0 or count <= 3:
        Cx = latitude(polygon[0])
        Cy = longitude(polygon[0])
        A=0
    else:
        Cx = (Cx/(6.0*A))
        Cy = (Cy/ (6.0*A))
    A = abs(A)
    return(Cx, Cy, A)
示例#22
0
def find_centroid(polygon):
    """Find the centroid of a polygon.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    polygon -- A list of positions, in which the first and last are the same

    Returns: 3 numbers; centroid latitude, centroid longitude, and polygon area

    Hint: If a polygon has 0 area, return its first position as its centroid

    >>> p1, p2, p3 = make_position(1, 2), make_position(3, 4), make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1]  # First vertex is also the last vertex
    >>> find_centroid(triangle)
    (3.0, 2.0, 6.0)
    >>> find_centroid([p1, p3, p2, p1])
    (3.0, 2.0, 6.0)
    >>> tuple(map(float, find_centroid([p1, p2, p1]))) # Forces result to be floats
    (1.0, 2.0, 0.0)
    """
    "*** YOUR CODE HERE ***"

    n = len(polygon) - 1
    A =  reduce(add, map(lambda x: latitude(polygon[x])*longitude(polygon[x+1])-\
        latitude(polygon[x+1])*longitude(polygon[x]), range(n)))/2
    if A == 0:
        return latitude(polygon[0]), longitude(polygon[0]), int(A)
    else:
        Cx = reduce(add, map(lambda x: (latitude(polygon[x])+latitude(polygon[x+1]))\
                          *(latitude(polygon[x])*longitude(polygon[x+1])-\
                            latitude(polygon[x+1])*longitude(polygon[x])), range(n)))/(6*A)
        Cy = reduce(add, map(lambda x: (longitude(polygon[x])+longitude(polygon[x+1]))\
                          *(latitude(polygon[x])*longitude(polygon[x+1])-\
                            latitude(polygon[x+1])*longitude(polygon[x])), range(n)))/(6*A)
    return (Cx, Cy, abs(A))
示例#23
0
def find_centroid(polygon):
    """
    Finds the centroid of a polygon.
    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon
    polygon -- A list of positions, in which the first and last are the same
    Returns: 3 numbers; centroid latitude, centroid longitude, and polygon area
    """
    area = 0
    x = 0
    y = 0
    for index in range(0, len(polygon) - 1):
        area += (latitude(polygon[index]) * longitude(polygon[index + 1])) - (
            latitude(polygon[index + 1]) * longitude(polygon[index]))
    if (area == 0):
        return latitude(polygon[0]), longitude(polygon[0]), 0
    for index in range(0, len(polygon) - 1):
        x += (latitude(polygon[index]) + latitude(polygon[index + 1])) * (
            (latitude(polygon[index]) * longitude(polygon[index + 1])) -
            (latitude(polygon[index + 1]) * longitude(polygon[index])))
        y += (longitude(polygon[index]) + longitude(polygon[index + 1])) * (
            (latitude(polygon[index]) * longitude(polygon[index + 1])) -
            (latitude(polygon[index + 1]) * longitude(polygon[index])))
    area = area / 2
    x = x / (6 * area)
    y = y / (6 * area)
    return (x, y, abs(area))
示例#24
0
 def compute_center_x(polygon):
     sum = 0
     for i in range(num_sides):
         expression1 = (latitude(polygon[i]) + latitude(polygon[i + 1]))
         expression2 = (latitude(polygon[i]) * longitude(polygon[i + 1]) -
                        latitude(polygon[i + 1]) * longitude(polygon[i]))
         # print("i = ",i," ",expression1," * ",expression2," = ", expression1*expression2)
         sum += expression1 * expression2
     if area == 0:
         return latitude(polygon[0])
     return sum / (6 * area)
示例#25
0
 def all_i_need(polygon):
     area_value, lat_value, lon_value = 0, 0, 0
     for _ in range(len(polygon) - 1):
         lat1, lat2, lon1, lon2 = latitude(polygon[_]), latitude(polygon[_ + 1]), longitude(polygon[_]), longitude(polygon[_ + 1])
         area_value += lat1 * lon2 - lat2 * lon1
         lat_value += (lat1 + lat2) * (lat1 * lon2 - lat2 * lon1)
         lon_value += (lon1 + lon2) * (lat1 * lon2 - lat2 * lon1)
     area_poly = .5 * area_value
     lat_poly = (1/(6 * area_poly)) * lat_value
     lon_poly = (1/(6 * area_poly)) * lon_value
     return -1 * lat_poly, -1 * lon_poly, abs(area_poly)
示例#26
0
文件: trends.py 项目: scottkd/trends
def signed_area(polygon):
    """Helper function for find_centroid

    polygon -- A list of positions, in which the first and last are the same

    Returns: polygon area
    """
    total = 0.0
    for i in range(0, len(polygon)-1):
        total += (latitude(polygon[i])*longitude(polygon[i+1]) - latitude(polygon[i+1])*longitude(polygon[i]))
    return 0.5*total
示例#27
0
 def area(p):
     A = 0
     n = len(p) - 1
     for i in range(n):
       plus_i = p[i+1]
       pos_i = p[i]
       xi = latitude(pos_i)
       yi = longitude(pos_i)
       xi_plus_1 = latitude(plus_i)
       yi_plus_1 = longitude(plus_i)
       A = A + (xi * yi_plus_1- xi_plus_1 * yi)
     return A/2
示例#28
0
def find_centroid(polygon):
    """Find the centroid of a polygon.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    polygon -- A list of positions, in which the first and last are the same

    Returns: 3 numbers; centroid latitude, centroid longitude, and polygon area

    Hint: If a polygon has 0 area, use the latitude and longitude of its first
    position as its centroid.

    >>> p1, p2, p3 = make_position(1, 2), make_position(3, 4), make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1]  # First vertex is also the last vertex
    >>> find_centroid(triangle)
    (3.0, 2.0, 6.0)
    >>> find_centroid([p1, p3, p2, p1])
    (3.0, 2.0, 6.0)
    >>> tuple(map(float, find_centroid([p1, p2, p1])))  # A zero-area polygon
    (1.0, 2.0, 0.0)
    """
    "*** YOUR CODE HERE***"
    s_area = 0.0
    cen_lat = 0.0
    cen_lon = 0.0

    #Sum over area
    for i in range(0, len(polygon) - 1):
        s_area += (latitude(polygon[i]) * longitude(polygon[i + 1])) - (
            latitude(polygon[i + 1]) * longitude(polygon[i]))

    #Half area
    s_area /= 2

    #Sum over for centroid latitude and longitude
    for i in range(0, len(polygon) - 1):
        cen_lat += (latitude(polygon[i]) + latitude(polygon[i + 1])) * s_area
        cen_lon += (longitude(polygon[i]) + longitude(polygon[i + 1])) * s_area

    #If zero-area return area as 0.0 and return latitude and longitude as is
    if (s_area == 0.0):
        s_area = 0.0
        cen_lat = latitude(polygon[0])
        cen_lon = longitude(polygon[0])

    #If has area, find centroid latitude, longitude
    else:
        cen_lat /= (s_area * 6.0)
        cen_lon /= (s_area * 6.0)

    #Return centroid lat, lon and area
    return cen_lat, cen_lon, abs(s_area)
示例#29
0
 def all_i_need(polygon):
     area_value, lat_value, lon_value = 0, 0, 0
     for _ in range(len(polygon) - 1):
         lat1, lat2, lon1, lon2 = latitude(polygon[_]), latitude(
             polygon[_ + 1]), longitude(polygon[_]), longitude(polygon[_ +
                                                                       1])
         area_value += lat1 * lon2 - lat2 * lon1
         lat_value += (lat1 + lat2) * (lat1 * lon2 - lat2 * lon1)
         lon_value += (lon1 + lon2) * (lat1 * lon2 - lat2 * lon1)
     area_poly = .5 * area_value
     lat_poly = (1 / (6 * area_poly)) * lat_value
     lon_poly = (1 / (6 * area_poly)) * lon_value
     return -1 * lat_poly, -1 * lon_poly, abs(area_poly)
示例#30
0
 def cy(p):
     count = 0
     n = len(p) - 1
     for i in range(n):
       plus_i = p[i+1]
       pos_i = p[i]
       xi = latitude(pos_i)
       yi = longitude(pos_i)
       xi_plus_1 = latitude(plus_i)
       yi_plus_1 = longitude(plus_i)
       count = count + (yi + yi_plus_1) * (xi * yi_plus_1 - xi_plus_1 * yi)
     A = area(polygon)
     return (1/(6*A)) * count
示例#31
0
def find_centroid(polygon):
    """Find the centroid of a polygon.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    polygon -- A list of positions, in which the first and last are the same

    Returns: 3 numbers; centroid latitude, centroid longitude, and polygon area

    Hint: If a polygon has 0 area, use the latitude and longitude of its first
    position as its centroid.

    >>> p1, p2, p3 = make_position(1, 2), make_position(3, 4), make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1]  # First vertex is also the last vertex
    >>> round5 = lambda x: round(x, 5) # Rounds floats to 5 digits
    >>> tuple(map(round5, find_centroid(triangle)))
    (3.0, 2.0, 6.0)
    >>> tuple(map(round5, find_centroid([p1, p3, p2, p1])))
    (3.0, 2.0, 6.0)
    >>> tuple(map(float, find_centroid([p1, p2, p1])))  # A zero-area polygon
    (1.0, 2.0, 0.0)
    """
    "*** YOUR CODE HERE ***"
    counter = 0
    x = 0
    y = 0
    a = 0
    the_latitude = 0
    the_longitude = 0
    the_area = 0
    while counter < len(polygon) - 1:
        current_x = latitude(polygon[counter])
        next_x = latitude(polygon[counter + 1])
        current_y = longitude(polygon[counter])
        next_y = longitude(polygon[counter + 1])
        x -= (current_x + next_x) * (current_x * next_y - next_x * current_y)
        y -= (current_y + next_y) * (current_x * next_y - next_x * current_y)
        a += (current_x * next_y - next_x * current_y)
        counter += 1
    if a != 0:
        if (a > 0 and (x < 0 or y < 0) or (a < 0 and (x > 0 or y > 0))):
            the_area = ( -1 * (a / 2))
            the_latitude = ((x) * (1 / (6 * the_area)))
            the_longitude = ((y) * (1 / (6 * the_area)))
            return ((the_latitude), (the_longitude), abs(the_area))
        the_area = abs((a / 2))
        the_latitude = (abs(x) * (1 / (6 * the_area)))
        the_longitude = (abs(y) * (1 / (6 * the_area)))
        return ((the_latitude), (the_longitude), abs(the_area))
    return (latitude(polygon[0]), longitude(polygon[0]), 0)
示例#32
0
 def calculate_summations(polygon, identifier): #1 identifier is for Area. #2 identifier is for centroid_x. #3 identifier is for centroid_y
     if polygon[1:]==[]:
         return 0
     first_point = polygon[0]
     second_point = polygon[1] 
     x_ident = latitude(first_point) + latitude(second_point)
     y_ident = longitude(first_point) + longitude(second_point)
     summation = latitude(first_point) * longitude(second_point) - latitude(second_point) * longitude(first_point)                           
     if identifier == 1:
         return summation + calculate_summations(polygon[1:], 1)
     elif identifier == 2:
         return summation * x_ident + calculate_summations(polygon[1:], 2)
     else:
         return summation * y_ident + calculate_summations(polygon[1:], 3)
示例#33
0
def find_centroid(polygon):
    """Find the centroid of a polygon.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    polygon -- A list of positions, in which the first and last are the same

    Returns: 3 numbers; centroid latitude, centroid longitude, and polygon area

    Hint: If a polygon has 0 area, return its first position as its centroid

    >>> p1, p2, p3 = make_position(1, 2), make_position(3, 4), make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1]  # First vertex is also the last vertex
    >>> find_centroid(triangle)
    (3.0, 2.0, 6.0)
    >>> find_centroid([p1, p3, p2, p1])
    (3.0, 2.0, 6.0)
    >>> find_centroid([p1, p2, p1])
    (1, 2, 0)
    """

    area, cx, cy, i = 0, 0, 0, 0

    while i < (len(polygon) - 1):

        x0 = latitude(polygon[i])
        y0 = longitude(polygon[i])
        x1 = latitude(polygon[i+1])
        y1 = longitude(polygon[i+1])

        sub = (x0 * y1) - (x1 * y0)
        area += sub
        cx += (x0 + x1) * (sub)
        cy += (y0 + y1) * (sub)

        i += 1

    area *= 0.5

    if area != 0:
        cx /= (6*area)
        cy /= (6*area)

    else:
        cx = latitude(polygon[0])
        cy = longitude(polygon[0])
        area = 0

    return (cx, cy, abs(area))
示例#34
0
文件: trends.py 项目: loganrudd/CS61A
def find_centroid(polygon):
    """Find the centroid of a polygon. If a polygon has 0 area, use the latitude
    and longitude of its first position as its centroid.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    Arguments:
    polygon -- A list of positions, in which the first and last are the same

    Returns 3 numbers: centroid latitude, centroid longitude, and polygon area.

    >>> p1 = make_position(1, 2)
    >>> p2 = make_position(3, 4)
    >>> p3 = make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1] # First vertex is also the last vertex
    >>> round_all = lambda s: [round(x, 5) for x in s]
    >>> round_all(find_centroid(triangle))
    [3.0, 2.0, 6.0]
    >>> round_all(find_centroid([p1, p3, p2, p1])) # reversed
    [3.0, 2.0, 6.0]
    >>> apply_to_all(float, find_centroid([p1, p2, p1])) # A zero-area polygon
    [1.0, 2.0, 0.0]
    """
    # lat and lon of 1st position
    x_c = [latitude(x) for x in polygon]
    y_c = [longitude(x) for x in polygon]
    i = 0
    total = 0
    while i < len(polygon) - 1:
        total = total + x_c[i] * y_c[i + 1] - x_c[i + 1] * y_c[i]
        i += 1
    area = abs(total / 2)
    if area == 0:
        return [latitude(polygon[0]), longitude(polygon[0])] + [area]
    i = 0
    total = 0
    while i < len(polygon) - 1:
        total = total + (x_c[i] + x_c[i + 1]) * (x_c[i] * y_c[i + 1] -
                                                 x_c[i + 1] * y_c[i])
        i += 1
    Cx = abs(total / (6 * area))
    i = 0
    total = 0
    while i < len(polygon) - 1:
        total = total + (y_c[i] + y_c[i + 1]) * (x_c[i] * y_c[i + 1] -
                                                 x_c[i + 1] * y_c[i])
        i += 1
    Cy = abs(total / (6 * area))
    return [Cx, Cy, area]
示例#35
0
def find_centroid(polygon):
    """Find the centroid of a polygon.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon
    
    polygon -- A tuple of positions, in which the first and last are the same

    Returns: 3 numbers; centroid latitude, centroid longitude, and polygon area

    Hint: If a polygon has 0 area, return its first position as its centroid

    >>> p1, p2, p3 = make_position(1, 2), make_position(3, 4), make_position(5, 0)
    >>> triangle = (p1, p2, p3, p1)  # First vertex is also the last vertex
    >>> find_centroid(triangle)
    (3.0, 2.0, 6.0)
    >>> find_centroid((p1, p3, p2, p1))
    (3.0, 2.0, 6.0)
    >>> find_centroid((p1, p2, p1))
    (1, 2, 0)
    """
    "*** YOUR CODE HERE ***"

    def Area(polygon):
        l = len(polygon)-1
        temp = 0
        area =0
        while(temp<l):
            area+= (latitude(polygon[temp]) *longitude(polygon[temp+1])-latitude(polygon[temp+1])*longitude(polygon[temp]))
            temp +=1
        area = area/2
        return (area)
    area = Area(polygon)

    l = len(polygon)-1
    temp = 0
    Cx, Cy = 0, 0

    while (temp <l):
        Cx += (latitude(polygon[temp])+latitude(polygon[temp+1]))*(latitude(polygon[temp]) *longitude(polygon[temp+1])-latitude(polygon[temp+1])*longitude(polygon[temp]))
        Cy += (longitude(polygon[temp])+longitude(polygon[temp+1]))*(latitude(polygon[temp]) *longitude(polygon[temp+1])-latitude(polygon[temp+1])*longitude(polygon[temp]))
        temp +=1

    if (area)==0:
        return latitude(polygon[0]), longitude(polygon[0]), 0

    Cx = Cx/(6*area)
    Cy = Cy/ (6*area)
    return Cx, Cy, abs(area)
示例#36
0
def find_centroid(polygon):
    """Find the centroid of a polygon. If a polygon has 0 area, use the latitude
    and longitude of its first position as its centroid.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    Arguments:
    polygon -- A list of positions, in which the first and last are the same

    Returns 3 numbers: centroid latitude, centroid longitude, and polygon area.

    >>> p1 = make_position(1, 2)
    >>> p2 = make_position(3, 4)
    >>> p3 = make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1] # First vertex is also the last vertex
    >>> round_all = lambda s: [round(x, 5) for x in s]
    >>> round_all(find_centroid(triangle))
    [3.0, 2.0, 6.0]
    >>> round_all(find_centroid([p1, p3, p2, p1])) # reversed
    [3.0, 2.0, 6.0]
    >>> apply_to_all(float, find_centroid([p1, p2, p1])) # A zero-area polygon
    [1.0, 2.0, 0.0]
    """
    area = 0
    i = 0
    while i < len(polygon) - 1:
        area = area + ((latitude(polygon[i]) * longitude(polygon[i + 1])) -
                       (latitude(polygon[i + 1]) * longitude(polygon[i])))
        i += 1
    area = area / 2

    if area == 0:
        return latitude(polygon[0]), longitude(polygon[0]), area

    def centroid_helper(fn):
        value = 0
        i = 0
        while i < len(polygon) - 1:
            value = value + (fn(polygon[i]) + fn(polygon[i + 1])) * (
                (latitude(polygon[i]) * longitude(polygon[i + 1])) -
                (latitude(polygon[i + 1]) * longitude(polygon[i])))
            i += 1
        return value / (6 * area)

    cx = centroid_helper(latitude)
    cy = centroid_helper(longitude)

    return cx, cy, abs(area)
示例#37
0
 def centroid_helper(fn):
     value = 0
     i = 0
     while i < len(polygon) - 1:
         value = value + (fn(polygon[i]) + fn(polygon[i+1]))*((latitude(polygon[i])*longitude(polygon[i+1])) - (latitude(polygon[i+1])*longitude(polygon[i])))
         i += 1
     return value/(6*area)
示例#38
0
def find_centroid(polygon):
    """Find the centroid of a polygon. If a polygon has 0 area, use the latitude
    and longitude of its first position as its centroid.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    Arguments:
    polygon -- A list of positions, in which the first and last are the same

    Returns 3 numbers: centroid latitude, centroid longitude, and polygon area.

    >>> p1 = make_position(1, 2)
    >>> p2 = make_position(3, 4)
    >>> p3 = make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1] # First vertex is also the last vertex
    >>> round_all = lambda s: [round(x, 5) for x in s]
    >>> round_all(find_centroid(triangle))
    [3.0, 2.0, 6.0]
    >>> round_all(find_centroid([p1, p3, p2, p1])) # reversed
    [3.0, 2.0, 6.0]
    >>> apply_to_all(float, find_centroid([p1, p2, p1])) # A zero-area polygon
    [1.0, 2.0, 0.0]
    """
    "*** YOUR CODE HERE ***"
    if len(polygon):
    	return [latitude(polygon[0]),longitude(polygon[0]),0]
    else:
示例#39
0
def min_distance(position, points):
    """Return a string with the closest dictionary key from a dictionary
    of points to a point."""
    x_1 = latitude(position)
    y_1 = longitude(position)
    min_dist = False
    for points_key in points:
        x_2 = latitude(points[points_key])
        y_2 = longitude(points[points_key])
        dist = pow(x_2 - x_1, 2)
        dist += pow(y_2 - y_1, 2)
        dist = pow(dist, 1 / 2)
        if not min_dist or dist < min_dist:
            min_dist = dist
            closest_key = points_key
    return closest_key
示例#40
0
def group_tweets_by_state(tweets):
    """Return a dictionary that groups tweets by their nearest state center.

    The keys of the returned dictionary are state names and the values are
    lists of tweets that appear closer to that state center than any other.

    Arguments:
    tweets -- a sequence of tweet abstract data types

    >>> sf = make_tweet("welcome to san francisco", None, 38, -122)
    >>> ny = make_tweet("welcome to new york", None, 41, -74)
    >>> two_tweets_by_state = group_tweets_by_state([sf, ny])
    >>> len(two_tweets_by_state)
    2
    >>> california_tweets = two_tweets_by_state['CA']
    >>> len(california_tweets)
    1
    >>> tweet_string(california_tweets[0])
    '"welcome to san francisco" @ (38, -122)'
    """
    lis=[]
    for elem in tweets:
        lat,lon=latitude(tweet_location(elem)),longitude(tweet_location(elem))
        state=closest_state(lat,lon)
        lis+=[[state, elem]]
    return group_by_key(lis)
示例#41
0
def find_centroid(polygon):
    """Find the centroid of a polygon.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    polygon -- A list of positions, in which the first and last are the same

    Returns: 3 numbers; centroid latitude, centroid longitude, and polygon area

    Hint: If a polygon has 0 area, use the latitude and longitude of its first
    position as its centroid.

    >>> p1, p2, p3 = make_position(1, 2), make_position(3, 4), make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1]  # First vertex is also the last vertex
    >>> round5 = lambda x: round(x, 5) # Rounds floats to 5 digits
    >>> tuple(map(round5, find_centroid(triangle)))
    (3.0, 2.0, 6.0)
    >>> tuple(map(round5, find_centroid([p1, p3, p2, p1])))
    (3.0, 2.0, 6.0)
    >>> tuple(map(float, find_centroid([p1, p2, p1])))  # A zero-area polygon
    (1.0, 2.0, 0.0)
    """
    "*** YOUR CODE HERE ***"
    element = 0
    area = 0
    x = 0.0
    y = 0.0
    vertices = len(polygon) -1
    while element < vertices: 
        x_i = latitude(polygon[element])
        x_i_plus1 = latitude(polygon[element + 1])
        y_i = longitude(polygon[element])
        y_i_plus1 = longitude(polygon[element + 1])
        area = area + (x_i * y_i_plus1) - (x_i_plus1 * y_i) 
        x = x + ((x_i + x_i_plus1) * (((x_i * y_i_plus1) - (x_i_plus1 * y_i))))
        y = y + ((y_i + y_i_plus1) * (((x_i * y_i_plus1) - (x_i_plus1 * y_i))))
        element += 1
    area = area / 2
    if area == 0:
        x = latitude(polygon[0])
        y = longitude(polygon[0])
    else:
        x = x / (area*6)
        y = y / (area*6)
    if area <= 0:
        area = abs(area)
    return x, y, area
示例#42
0
def find_centroid(polygon):
    """Find the centroid of a polygon.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    polygon -- A list of positions, in which the first and last are the same

    Returns: 3 numbers; centroid latitude, centroid longitude, and polygon area

    Hint: If a polygon has 0 area, return its first position as its centroid

    >>> p1, p2, p3 = make_position(1, 2), make_position(3, 4), make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1]  # First vertex is also the last vertex
    >>> find_centroid(triangle)
    (3.0, 2.0, 6.0)
    >>> find_centroid([p1, p3, p2, p1])
    (3.0, 2.0, 6.0)
    >>> find_centroid([p1, p2, p1])
    (1, 2, 0.0)
    """
    "*** YOUR CODE HERE ***"
    polygon = polygon + [
        polygon[0]
    ]  # add first vertex to end but in a way to not change original list

    area, cx, cy = 0, 0, 0

    for i in range(len(polygon) - 1):
        x1 = latitude(polygon[i])
        x2 = latitude(polygon[i + 1])
        y1 = longitude(polygon[i])
        y2 = longitude(polygon[i + 1])

        cx += (x1 + x2) * (x1 * y2 - x2 * y1)
        cy += (y1 + y2) * (x1 * y2 - x2 * y1)
        area += x1 * y2 - x2 * y1

    area /= 2

    if area == 0:
        cx = latitude(polygon[0])
        cy = longitude(polygon[0])
    else:
        cx /= (6 * area)
        cy /= (6 * area)

    return cx, cy, abs(area)
示例#43
0
文件: trends.py 项目: nirshtern/CS61A
def find_centroid(polygon):
    """Find the centroid of a polygon.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    polygon -- A list of positions, in which the first and last are the same

    Returns: 3 numbers; centroid latitude, centroid longitude, and polygon area

    Hint: If a polygon has 0 area, use the latitude and longitude of its first
    position as its centroid.

    >>> p1, p2, p3 = make_position(1, 2), make_position(3, 4), make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1]  # First vertex is also the last vertex
    >>> round5 = lambda x: round(x, 5) # Rounds floats to 5 digits
    >>> tuple(map(round5, find_centroid(triangle)))
    (3.0, 2.0, 6.0)
    >>> tuple(map(round5, find_centroid([p1, p3, p2, p1])))
    (3.0, 2.0, 6.0)
    >>> tuple(map(float, find_centroid([p1, p2, p1])))  # A zero-area polygon
    (1.0, 2.0, 0.0)
    """

    #Helper function to calculate the area
    def area_point_calc(p1,p2):
        return (latitude(p1)*longitude(p2))-(latitude(p2)*longitude(p1))

    #Helper function to calculate the latitude of the centroid
    def lat_center(p1,p2):
        return (latitude(p1)+latitude(p2))*(latitude(p1)*longitude(p2)-latitude(p2)*longitude(p1))

    #Helper function to calculate the longitude of the centroid
    def lon_center(p1,p2):
        return (longitude(p1)+longitude(p2))*(latitude(p1)*longitude(p2)-latitude(p2)*longitude(p1))

    area=0
    i=0
    centroid_x=0
    centroid_y=0
    
    #Sum up all the areas, centroid_x and centroid_y values
    while i < len(polygon)-1:
        area        += area_point_calc(polygon[i],polygon[i+1])
        centroid_x  += lat_center(polygon[i],polygon[i+1])
        centroid_y  += lon_center(polygon[i],polygon[i+1])
        i           += 1

    area=area/2

    if area==0:
        centroid_x=latitude(polygon[0])
        centroid_y=longitude(polygon[0])

    #Finish calculating the final values of centroid_x and centroid_y
    else:
        centroid_x= centroid_x / (6 * area)
        centroid_y= centroid_y / (6 * area)

    return (centroid_x, centroid_y, abs(area))
示例#44
0
def find_centroid(polygon):
    """Find the centroid of a polygon.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    polygon -- A list of positions, in which the first and last are the same

    Returns: 3 numbers; centroid latitude, centroid longitude, and polygon area

    Hint: If a polygon has 0 area, use the latitude and longitude of its first
    position as its centroid.

    >>> p1, p2, p3 = make_position(1, 2), make_position(3, 4), make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1]  # First vertex is also the last vertex
    >>> round5 = lambda x: round(x, 5) # Rounds floats to 5 digits
    >>> tuple(map(round5, find_centroid(triangle)))
    (3.0, 2.0, 6.0)
    >>> tuple(map(round5, find_centroid([p1, p3, p2, p1])))
    (3.0, 2.0, 6.0)
    >>> tuple(map(float, find_centroid([p1, p2, p1])))  # A zero-area polygon
    (1.0, 2.0, 0.0)
    """
    "*** YOUR CODE HERE ***"

    c_lat = 0
    c_lon = 0
    area = 0

    for x in range(len(polygon)-1):
        pos0 = polygon[x]
        pos1 = polygon[x+1]
        fac = (latitude(pos0) * longitude(pos1)) - \
              (latitude(pos1) * longitude(pos0))

        area += fac
        c_lat += (latitude(pos0) + latitude(pos1)) * fac
        c_lon += (longitude(pos0) + longitude(pos1)) * fac

    if area == 0:
        return (latitude(polygon[0]), longitude(polygon[0]), 0)

    area = area / 2
    c_lat = c_lat / (6 * area)
    c_lon = c_lon / (6 * area)

    return (c_lat, c_lon, abs(area))
示例#45
0
def find_centroid(polygon):
    """Find the centroid of a polygon.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    polygon -- A list of positions, in which the first and last are the same

    Returns: 3 numbers; centroid latitude, centroid longitude, and polygon area

    Hint: If a polygon has 0 area, use the latitude and longitude of its first
    position as its centroid.

    >>> p1, p2, p3 = make_position(1, 2), make_position(3, 4), make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1]  # First vertex is also the last vertex
    >>> round5 = lambda x: round(x, 5) # Rounds floats to 5 digits
    >>> tuple(map(round5, find_centroid(triangle)))
    (3.0, 2.0, 6.0)
    >>> tuple(map(round5, find_centroid([p1, p3, p2, p1])))
    (3.0, 2.0, 6.0)
    >>> tuple(map(float, find_centroid([p1, p2, p1])))  # A zero-area polygon
    (1.0, 2.0, 0.0)
    """
    "*** YOUR CODE HERE ***"
    area = 0
    i = 0
    c_x = 0
    c_y = 0

    while i < (len(polygon) - 1):
        area += (latitude(polygon[i]) * longitude(polygon[i + 1]) -
                 latitude(polygon[i + 1]) * longitude(polygon[i]))
        i = 1 + i

    area = 1 / 2 * (area)
    if area == 0:
        return latitude(polygon[0]), longitude(polygon[0]), 0

    else:
        i = 0
        while i < (len(polygon) - 1):
            c_x += (latitude(polygon[i]) + latitude(polygon[i + 1])) * (
                (latitude(polygon[i]) * longitude(polygon[i + 1]) -
                 latitude(polygon[i + 1]) * longitude(polygon[i])))
            c_y += (longitude(polygon[i]) + longitude(polygon[i + 1])) * (
                (latitude(polygon[i]) * longitude(polygon[i + 1]) -
                 latitude(polygon[i + 1]) * longitude(polygon[i])))
            i += 1

        c_x = ((1 / (6 * area))) * c_x
        c_y = ((1 / (6 * area))) * c_y
        return c_x, c_y, abs(area)
def find_centroid(polygon):
    """Find the centroid of a polygon.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    polygon -- A list of positions, in which the first and last are the same

    Returns: 3 numbers; centroid latitude, centroid longitude, and polygon area

    Hint: If a polygon has 0 area, use the latitude and longitude of its first
    position as its centroid.

    >>> p1, p2, p3 = make_position(1, 2), make_position(3, 4), make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1]  # First vertex is also the last vertex
    >>> round5 = lambda x: round(x, 5) # Rounds floats to 5 digits
    >>> tuple(map(round5, find_centroid(triangle)))
    (3.0, 2.0, 6.0)
    >>> tuple(map(round5, find_centroid([p1, p3, p2, p1])))
    (3.0, 2.0, 6.0)
    >>> tuple(map(float, find_centroid([p1, p2, p1])))  # A zero-area polygon
    (1.0, 2.0, 0.0)
    """
    "*** YOUR CODE HERE ***"
    area = 0
    def mathhelper(n, polygon):
        x=latitude(polygon[n])
        y=longitude(polygon[n])
        x2=latitude(polygon[n+1])
        y2=longitude(polygon[n+1])
        return x*y2-x2*y
    for n in range(len(polygon)-1):
        #1/2*(x*y2+x2*y)
        area+=0.5*mathhelper(n,polygon)
    if area==0:
        center_x=latitude(polygon[0])
        center_y=longitude(polygon[0])
    else:
        center_x=0
        center_y=0
        for n in range(len(polygon)-1):
            #1/6a*(x+x2)*(x*y2+x2*y)
            center_x+=(latitude(polygon[n])+latitude(polygon[n+1]))*(mathhelper(n,polygon))/(6*area)
            #1/6a*(y+y2)*(x*y2+x2*y)
            center_y+=(longitude(polygon[n])+longitude(polygon[n+1]))*(mathhelper(n,polygon))/(6*area)

    return center_x, center_y, abs(area)
示例#47
0
def find_centroid(polygon):
    """Find the centroid of a polygon.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    polygon -- A list of positions, in which the first and last are the same

    Returns: 3 numbers; centroid latitude, centroid longitude, and polygon area

    Hint: If a polygon has 0 area, return its first position as its centroid

    >>> p1, p2, p3 = make_position(1, 2), make_position(3, 4), make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1]  # First vertex is also the last vertex
    >>> find_centroid(triangle)
    (3.0, 2.0, 6.0)
    >>> find_centroid([p1, p3, p2, p1])
    (3.0, 2.0, 6.0)
    >>> find_centroid([p1, p2, p1])
    (1, 2, 0)
    """
    area,coor_x,coor_y=0,0,0
    for i in range(0,len(polygon)-1):
            area+=(abs(latitude(polygon[i]))*abs(longitude(polygon[i+1]))-abs(latitude(polygon[i+1]))*abs(longitude(polygon[i])))/2
            coor_x+=(latitude(polygon[i])+latitude(polygon[i+1]))*(latitude(polygon[i])*longitude(polygon[i+1])-latitude(polygon[i+1])*longitude(polygon[i]))/6
            coor_y+=(longitude(polygon[i])+longitude(polygon[i+1]))*(latitude(polygon[i])*longitude(polygon[i+1])-latitude(polygon[i+1])*longitude(polygon[i]))/6
    if area==0:
        return (latitude(polygon[0]),longitude(polygon[0]),0)
    return (coor_x/area,coor_y/area,abs(area))
示例#48
0
def find_centroid(polygon):
    """Find the centroid of a polygon. If a polygon has 0 area, use the latitude
    and longitude of its first position as its centroid.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    Arguments:
    polygon -- A list of positions, in which the first and last are the same

    Returns 3 numbers: centroid latitude, centroid longitude, and polygon area.

    >>> p1 = make_position(1, 2)
    >>> p2 = make_position(3, 4)
    >>> p3 = make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1] # First vertex is also the last vertex
    >>> round_all = lambda s: [round(x, 5) for x in s]
    >>> round_all(find_centroid(triangle))
    [3.0, 2.0, 6.0]
    >>> round_all(find_centroid([p1, p3, p2, p1])) # reversed
    [3.0, 2.0, 6.0]
    >>> apply_to_all(float, find_centroid([p1, p2, p1])) # A zero-area polygon
    [1.0, 2.0, 0.0]
    """
    # get a value for n in the equation on Wikipedia            // n = len(polygon) - 1
    # compute area                                              // use a for loop "for i in range(n)", implement formula for area
    # check if area is zero; if yes, set to first position      // set x to latitude(polygon[0]) and y to longitude(polygon[0])
    # if not, compute the values of x and y                     // use a for loop "for i in range(n)", implement formulae for x and y
    # return list of x, y and area                              // make sure to take the absolute value of area

    n = len(polygon) - 1
    area = 0
    x = 0
    y = 0
    for i in range(n):
        area += (latitude(polygon[i]) * longitude(polygon[i + 1]) -
                 latitude(polygon[i + 1]) * longitude(polygon[i])) / 2
    if area == 0:
        x = latitude(polygon[0])
        y = longitude(polygon[0])
    else:
        for i in range(n):
            x += ((latitude(polygon[i]) + latitude(polygon[i + 1])) *
                  (latitude(polygon[i]) * longitude(polygon[i + 1]) -
                   latitude(polygon[i + 1]) * longitude(polygon[i]))) / (6 *
                                                                         area)
            y += ((longitude(polygon[i]) + longitude(polygon[i + 1])) *
                  (latitude(polygon[i]) * longitude(polygon[i + 1]) -
                   latitude(polygon[i + 1]) * longitude(polygon[i]))) / (6 *
                                                                         area)
    return [x, y, abs(area)]
示例#49
0
def find_centroid(polygon):
    """Find the centroid of a polygon.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    polygon -- A list of positions, in which the first and last are the same

    Returns: 3 numbers; centroid latitude, centroid longitude, and polygon area

    Hint: If a polygon has 0 area, use the latitude and longitude of its first
    position as its centroid.

    >>> p1, p2, p3 = make_position(1, 2), make_position(3, 4), make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1]  # First vertex is also the last vertex
    >>> find_centroid(triangle)
    (3.0, 2.0, 6.0)
    >>> find_centroid([p1, p3, p2, p1])
    (3.0, 2.0, 6.0)
    >>> tuple(map(float, find_centroid([p1, p2, p1])))  # A zero-area polygon
    (1.0, 2.0, 0.0)
    """
    "*** YOUR CODE HERE ***"
    poly = len(polygon)-1
    area = 0
    for i in range(poly):
        area += (0.5)*(latitude(polygon[i])*longitude(polygon[i+1])-(latitude(polygon[i+1])*longitude(polygon[i])))
        if area == 0:
            return (latitude(polygon[0]), longitude(polygon[0]), 0)
    for i in range(poly):
        Cx = (latitude(polygon[i]) + latitude(polygon[i+1]))*(latitude(polygon[i])*longitude(polygon[i+1]) - latitude(polygon[i])*longitude(polygon[i]))
        
        Cy = (longitude(polygon[i]) + longitude(polygon[i+1]))*(latitude(polygon[i])*longitude(polygon[i+1]) - latitude(polygon[i+1])*longitude(polygon[i]))
    return ((1/6*area)*Cx),((1/6*area)*Cy),abs(area)
示例#50
0
文件: trends.py 项目: chengeaa/cs61a
def find_centroid(polygon):
    """Find the centroid of a polygon.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    polygon -- A list of positions, in which the first and last are the same

    Returns: 3 numbers; centroid latitude (x), centroid longitude(y), and polygon area

    Hint: If a polygon has 0 area, use the latitude and longitude of its first
    position as its centroid.

    >>> p1, p2, p3 = make_position(1, 2), make_position(3, 4), make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1]  # First vertex is also the last vertex
    >>> round5 = lambda x: round(x, 5) # Rounds floats to 5 digits
    >>> list(map(round5, find_centroid(triangle)))
    [3.0, 2.0, 6.0]
    >>> list(map(round5, find_centroid([p1, p3, p2, p1])))
    [3.0, 2.0, 6.0]
    >>> list(map(float, find_centroid([p1, p2, p1])))  # A zero-area polygon
    [1.0, 2.0, 0.0]
    """
    "*** YOUR CODE HERE ***"
    cx, cy, area, multiplyer = 0, 0, 0, 0 
    for i in range(len(polygon) -1):
        cx += ((latitude(polygon[i]) + latitude(polygon[i + 1])) * (latitude(polygon[i]) * longitude(polygon[i + 1]) - longitude(polygon[i]) * latitude(polygon[i + 1])))
        cy += ((longitude(polygon[i]) + longitude(polygon[i + 1])) * (latitude(polygon[i]) * longitude(polygon[i + 1]) - longitude(polygon[i]) * latitude(polygon[i + 1])))
        area += (latitude(polygon[i]) * longitude(polygon[i + 1])) - (latitude(polygon[i + 1]) * longitude(polygon[i]))
    area = area * (1/2)
    if area == 0:
        return latitude(polygon[0]), longitude(polygon[0]), area 
    cx = (cx / (6 * area))
    cy = (cy / (6 * area))
    return [cx, cy, abs(area)]
示例#51
0
def find_centroid(polygon):
    """Find the centroid of a polygon.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    polygon -- A list of positions, in which the first and last are the same

    Returns: 3 numbers; centroid latitude, centroid longitude, and polygon area

    Hint: If a polygon has 0 area, use the latitude and longitude of its first
    position as its centroid.

    >>> p1, p2, p3 = make_position(1, 2), make_position(3, 4), make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1]  # First vertex is also the last vertex
    >>> round5 = lambda x: round(x, 5) # Rounds floats to 5 digits
    >>> list(map(round5, find_centroid(triangle)))
    [3.0, 2.0, 6.0]
    >>> list(map(round5, find_centroid([p1, p3, p2, p1])))
    [3.0, 2.0, 6.0]
    >>> list(map(float, find_centroid([p1, p2, p1])))  # A zero-area polygon
    [1.0, 2.0, 0.0]
    """
    "*** YOUR CODE HERE ***"
    cx_minus_coefficient = 0
    cy_minus_coefficient = 0
    double_area = 0
    for i in range(len(polygon)):
        if i <= (len(polygon) - 2):
            double_area += (latitude(polygon[i]) * longitude(polygon[i + 1]) -
                            latitude(polygon[i + 1]) * longitude(polygon[i]))
    area = double_area * (1 / 2)
    if area == 0:  #a zero area polygon
        cx = latitude(polygon[0])
        cy = longitude(polygon[0])
    else:
        for i in range(len(polygon)):
            if i <= (len(polygon) - 2):
                cx_minus_coefficient += (
                    latitude(polygon[i]) + latitude(polygon[i + 1])) * (
                        latitude(polygon[i]) * longitude(polygon[i + 1]) -
                        latitude(polygon[i + 1]) * longitude(polygon[i]))
        cx = (1 / (6 * area)) * cx_minus_coefficient
        for i in range(len(polygon)):
            if i <= (len(polygon) - 2):
                cy_minus_coefficient += (
                    longitude(polygon[i]) + longitude(polygon[i + 1])) * (
                        latitude(polygon[i]) * longitude(polygon[i + 1]) -
                        latitude(polygon[i + 1]) * longitude(polygon[i]))
        cy = (1 / (6 * area)) * cy_minus_coefficient
    return cx, cy, abs(area)
def find_centroid(polygon):
    """Find the centroid of a polygon.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    polygon -- A list of positions, in which the first and last are the same

    Returns: 3 numbers; centroid latitude, centroid longitude, and polygon area

    Hint: If a polygon has 0 area, use the latitude and longitude of its first
    position as its centroid.

    >>> p1, p2, p3 = make_position(1, 2), make_position(3, 4), make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1]  # First vertex is also the last vertex
    >>> round5 = lambda x: round(x, 5) # Rounds floats to 5 digits
    >>> tuple(map(round5, find_centroid(triangle)))
    (3.0, 2.0, 6.0)
    >>> tuple(map(round5, find_centroid([p1, p3, p2, p1])))
    (3.0, 2.0, 6.0)
    >>> tuple(map(float, find_centroid([p1, p2, p1])))  # A zero-area polygon
    (1.0, 2.0, 0.0)
    """
    "*** YOUR CODE HERE ***"
    sum_area = 0
    sum_lat = 0
    sum_lon = 0
    length = len(polygon)

    for i in range(0, length - 1):
        pos_i = polygon[i]
        pos_i2 = polygon[i + 1]
        common = latitude(pos_i) * longitude(pos_i2) - latitude(
            pos_i2) * longitude(pos_i)
        sum_area += common
        sum_lat += (latitude(pos_i) + latitude(pos_i2)) * common
        sum_lon += (longitude(pos_i) + longitude(pos_i2)) * common
    area = abs(sum_area / 2)
    if (area == 0):
        lat = latitude(polygon[0])
        lon = longitude(polygon[0])
    else:
        lat = sum_lat / 6 / sum_area * 2
        lon = sum_lon / 6 / sum_area * 2
    return lat, lon, area
def find_centroid(polygon):
    """Find the centroid of a polygon.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    polygon -- A list of positions, in which the first and last are the same

    Returns: 3 numbers; centroid latitude, centroid longitude, and polygon area

    Hint: If a polygon has 0 area, return its first position as its centroid

    >>> p1, p2, p3 = make_position(1, 2), make_position(3, 4), make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1]  # First vertex is also the last vertex
    >>> find_centroid(triangle)
    (3.0, 2.0, 6.0)
    >>> find_centroid([p1, p3, p2, p1])
    (3.0, 2.0, 6.0)
    >>> find_centroid([p1, p2, p1])
    (1, 2, 0)
    """
    total = 0

    for i in range(len(polygon) - 1):
        total += (latitude(polygon[i]) * longitude(polygon[i + 1])) - (
            latitude(polygon[i + 1]) * longitude(polygon[i]))
    area = total / 2

    if area == 0:
        return (latitude(polygon[0]), longitude(polygon[0]), 0)
    total = 0

    for i in range(len(polygon) - 1):
        total += (latitude(polygon[i]) + latitude(polygon[i + 1])) * (
            (latitude(polygon[i]) * longitude(polygon[i + 1])) -
            (latitude(polygon[i + 1]) * longitude(polygon[i])))
    centroid_latitude = total / (6 * area)

    total = 0

    for i in range(len(polygon) - 1):
        total += (longitude(polygon[i]) + longitude(polygon[i + 1])) * (
            (latitude(polygon[i]) * longitude(polygon[i + 1])) -
            (latitude(polygon[i + 1]) * longitude(polygon[i])))
    centroid_longitude = total / (6 * area)

    if area < 0:
        return (centroid_latitude, centroid_longitude, -area)
    else:
        return (centroid_latitude, centroid_longitude, area)
示例#54
0
def find_centroid(polygon):
    """Find the centroid of a polygon. If a polygon has 0 area, use the latitude
    and longitude of its first position as its centroid.

    http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

    Arguments:
    polygon -- A list of positions, in which the first and last are the same

    Returns 3 numbers: centroid latitude, centroid longitude, and polygon area.

    >>> p1 = make_position(1, 2)
    >>> p2 = make_position(3, 4)
    >>> p3 = make_position(5, 0)
    >>> triangle = [p1, p2, p3, p1] # First vertex is also the last vertex
    >>> round_all = lambda s: [round(x, 5) for x in s]
    >>> round_all(find_centroid(triangle))
    [3.0, 2.0, 6.0]
    >>> round_all(find_centroid([p1, p3, p2, p1])) # reversed
    [3.0, 2.0, 6.0]
    >>> apply_to_all(float, find_centroid([p1, p2, p1])) # A zero-area polygon
    [1.0, 2.0, 0.0]
    """
    "*** YOUR CODE HERE ***"
    length = len(polygon) - 1  #as last element same as first
    centroid_lat, centroid_long, area, i = 0, 0, 0, 0
    while i < length:  #formula from Wikipedia
        current = polygon[i]
        next = polygon[i + 1]
        common = (latitude(current) * longitude(next) -
                  latitude(next) * longitude(current)
                  )  # The common term in the formula for area, lat and lon
        centroid_lat += (latitude(current) + latitude(next)) * common
        centroid_long += (longitude(current) + longitude(next)) * common
        area += 0.5 * common
        i += 1

    if area == 0:
        return latitude(polygon[0]), longitude(polygon[0]), area

    centroid_lat = centroid_lat / (6 * area)
    centroid_long = centroid_long / (6 * area)
    return [centroid_lat, centroid_long, abs(area)]