Created
May 17, 2020 18:37
-
-
Save level09/e22770bb1c5ce70303c2fd47383d4a61 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -------------------------------- using numpy | |
from __future__ import print_function | |
import math | |
import numpy as np | |
from numpy import trapz | |
def solution(x, y): | |
return trapz(y, x) | |
# -------------------------------- Manually calculate triangles and sum them up | |
x = np.array([0.00, 0.2, 0.33, 0.43, 0.63, 0.66, 1]) | |
y = np.array([0, 0.25, 0.25, 0.50, 0.50, 1, 1]) | |
def rect(x, y): | |
return x * y | |
def distance(x1, y1, x2, y2): | |
return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) | |
def getWidthHeight(x1, y1, x2, y2): | |
width = distance(x1, 0, x2, 0) | |
height = distance(0, y1, 0, y2) | |
return width, height | |
def trpz(x, y): | |
rects = 0 | |
for i in range(len(x) - 2): | |
# get width/heights | |
print (x[i], y[i], x[i + 1], y[i + 1]) | |
width, height = getWidthHeight(x[i], y[i], x[i + 1], y[i + 1]) | |
print ('width/height ', width, height) | |
# get area | |
area = width * height | |
print('area ', area) | |
# add to sum | |
rects += area | |
return area | |
print (trpz(x,y)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment