Commit 07690244 by Jigyasa Watwani

fixed bug in perimeter calculation

parent a4b8cfc4
Showing with 15 additions and 26 deletions
...@@ -2,27 +2,13 @@ import numpy as np ...@@ -2,27 +2,13 @@ import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import pandas as pd import pandas as pd
from matplotlib.widgets import Slider from matplotlib.widgets import Slider
from scipy.spatial import ConvexHull
def perimeter(X, Y, n): def perimeter(X, Y):
# Initialize perimeter points = np.column_stack((X, Y))
perimeter = 0 hull = ConvexHull(points)
value = sum(np.linalg.norm(points[hull.vertices[i]] - points[hull.vertices[(i+1)%len(hull.vertices)]]) for i in range(len(hull.vertices)))
# Calculate distance between each pair of consecutive points return value
for j in range(n):
# Get current point
x1, y1 = X[j], Y[j]
# Get next point (if i is the last index, get the first point)
if j == n-1:
x2, y2 = X[0], Y[0]
else:
x2, y2 = X[j+1], Y[j+1]
# Calculate distance between points and add to perimeter
perimeter += np.sqrt((x2-x1)**2 + (y2-y1)**2)
return perimeter
def long_axis(x, y): def long_axis(x, y):
x_diff = x[:, None] - x x_diff = x[:, None] - x
...@@ -31,13 +17,16 @@ def long_axis(x, y): ...@@ -31,13 +17,16 @@ def long_axis(x, y):
return np.max(distances) return np.max(distances)
# NOTE: check perimeter and long_axis functions on known shapes # NOTE: check perimeter and long_axis functions on known shapes
# x = np.array([0, 1, 0, -1]) # x = np.array([1, 0, -1, 0])
# y = np.array([2, 0, -2, 0]) # y = np.array([0, 2, 0, -2])
# print(x, y) # print(long_axis(x, y))
# print(long_axis(x, y))
# points = np.column_stack((x, y))
# hull = ConvexHull(points)
# print(long_axis(x, y))
# raise SystemExit # raise SystemExit
file = np.loadtxt('xy_coordinates_movie1.txt') file = np.loadtxt('xy_coordinates_movie15.txt')
number_of_pixels = file.shape[0] number_of_pixels = file.shape[0]
time_points = file.shape[1] time_points = file.shape[1]
time_array = np.arange(0, 5*int(time_points/2), 5) time_array = np.arange(0, 5*int(time_points/2), 5)
...@@ -95,7 +84,7 @@ plt.show() ...@@ -95,7 +84,7 @@ plt.show()
perimeter_array = np.zeros(int(time_points/2)) perimeter_array = np.zeros(int(time_points/2))
for k in range(0, int(time_points/2)): for k in range(0, int(time_points/2)):
perimeter_array[k] = perimeter(x[k], y[k], number_of_pixels) perimeter_array[k] = perimeter(x[k], y[k])
plt.plot(time_array, perimeter_array, marker='o', linestyle='None') plt.plot(time_array, perimeter_array, marker='o', linestyle='None')
plt.xlabel('Time') plt.xlabel('Time')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment