Apr-05-2022, 10:54 PM
(Jan-18-2021, 08:39 AM)paul18fr Wrote: You can easily check using the example of the website I gave you.
My advices: use vectorization instead of a loop, and above all, do not use append (dynamic memory allocation is time consuming); have a look to the code here bellow as an example.
Of course by replacing and increasing the n value, the area tends to 1
import numpy as np import matplotlib.pyplot as plt import matplotlib.tri as mtri axes=plt.axes() n = 10_000 # x=np.random.random(n) # y=np.random.random(n) x= np.array( [ 15, 23, 50] ) # see https://www.mathopenref.com/coordtrianglearea.html y= np.array( [ 15, 30, 25] ) # Area = 222.5 for these 3 vertices axes.set_xlim([0,1]) axes.set_ylim([0,1]) # plt.scatter(x,y) # plt.show() plt.scatter(x,y) plt.show() # Delanuay Triangulation method triang = mtri.Triangulation(x, y) plt.triplot(triang, 'ro:') VNN = triang.triangles # print(VNN) a,b=VNN.shape i = np.arange(a) # formula: Area = 0.5*| (Xb-Xa)(Yc-Ya)-(Xc-Xa)(Yb-Ya) | where a,b,c are 3 vertices Area = np.sum( np.abs (0.5*( (x[VNN[i,1]]-x[VNN[i,0]])*(y[VNN[i,2]]-y[VNN[i,0]]) - \ (x[VNN[i,2]]-x[VNN[i,0]])*(y[VNN[i,1]]-y[VNN[i,0]]) ))) print("Area = {}".format(Area))
I do not understand the reason for saying "Of course by replacing and increasing the n value, the area tends to 1" and so for having n = 10_000. Could you elaborate more?
Thanks,
Valerio