Using Scipy.spatial.Delaunay to realize the construction of triangular network

Source: Internet
Author: User

1. Preface

It is well known that Delaunay triangulation algorithm has an important application in surveying and mapping engineering. If you are using C#,java's programming language, I'm sorry you have to implement this algorithm yourself. Fortunately, Python has a very powerful open source library, Python+numpy+scipy+matplotlib is a scientific computing "golden Partner", of course, such as pandas flow of high-performance data Analysis Library, master them you do not have to re-build wheels.

2. Go to the Chase

This essay mainly describes how to use Python+scipy+numpy+matplotlib to demonstrate the construction of the Delaunay Triangle Network and ultimately to display it graphically.

    • First put the main data structure code:
classvector2d ():def __init__(self, x, Y, name=""): Self.name=name self.x=x self.y=y Self.rnx_path=None#Overloaded Operators    def __eq__(self, Other):ifIsinstance (Other, types. Nonetype)or  notIsinstance (Other, vector2d):RaiseValueError ('expected type of:%s'%(Type (vector2d)))Else:            return(other.x = = self.x) and(Other.y = =self.y)def __add__(self, Other):ifIsinstance (Other, types. Nonetype)or  notIsinstance (Other, vector2d):RaiseValueError ('expected type of:%s'%(Type (vector2d)))Else:            returnVector2d (self.x + other.x, Self.y +other.y)def __sub__(self, Other):ifIsinstance (Other, types. Nonetype)or  notIsinstance (Other, vector2d):RaiseValueError ('expected type of:%s'%(Type (vector2d)))Else:            returnVector2d (self.x-other.x, Self.y-other.y)#overloading multiplication to cross product    def __mul__(self, Other):ifIsinstance (Other, types. Nonetype)or  notIsinstance (Other, vector2d):RaiseValueError ('expected type of:%s'%(Type (vector2d)))Else:            returnOther.x * self.y-self.x *other.y @propertydefLength (self):returnMATH.SQRT (self.x * * 2 + self.y * * 2)
def __str__(self):return '(%s,%s,%s)'% (Self.name, self.x, SELF.Y)

The main function of this class is to represent two-dimensional vectors, which you can also interpret as two-dimensional points. The implementation is primarily overloaded with several operators.

classTriangle ():" "the type of the formal parameter point is vector2d" "    def __init__(self, point1, Point2, Point3): Self.p1=point1 self.p2=Point2 self.p3=Point3def __str__(self):return '%s->%s->%s'%(Self.p1.name, Self.p2.name, Self.p3.name)defIs_in_triangle (self, point):ifIsinstance (point, types. Nonetype)or  notisinstance (Point, vector2d):RaiseValueError ('expected type of:%s'%(Type (vector2d)))Else: o2a= Self.p1-Point o2b= SELF.P2-Point o2c= SELF.P3- Pointreturn((O2A * o2b > 0 andO2B * o2c > 0 andO2C * o2a > 0)or(O2A * O2B < 0 andO2B * O2C < 0 andO2C * O2A < 0))

This class is mainly used to denote triangular elements, not to mention it.

    • Here is the key to generate the triangulation section of the code, first you must first import Delaunay
 from  scipy.spatial import  Delaunay 
 def   Triangulate (vertex):   "  Get Delauney triangles from points:p Aram Vertex:list of Vector2d:ret Urn:delauney triangles    triangles = [] Delauney  = Delaunay ([[Pt.x, Pt.y] for
       pt in   vertex]). Simplices.copy ()  Span style= "color: #0000ff;" >for  tri in   Delauney:triangles.append (T Riangle (Vertex[tri[0]], vertex[tri[ 1]], Vertex[tri[2 return  Triangles,delauney 

The Triangulate method's formal parameter is the list type of the Vector2d class. The Delaunay method returns all the triangular elements and the object. Delaunay is used when drawing with matplotlib.

    • Drawing
 for  in vertex])
Import Matplotlib.pyplot as Plt Import Matplotlib.tri as Tri Import NumPy as Npplt.triplot (points[:,0], points[:,1], Delaunay, linewidth=1.5)
Plt.show ()

Using Scipy.spatial.Delaunay to realize the construction of triangular network

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.