Best Python code snippet using Airtest
Delunay_Triangulation_Quad_Edge.py
Source:Delunay_Triangulation_Quad_Edge.py
1from HALF_EDGE.half_edge import *2from QUAD_EDGE.quad_edge import *3456class Stack:7 def __init__(self):8 self.items = []910 def isEmpty(self):11 return self.items == []1213 def push(self, item):14 self.items.append(item)1516 def pop(self):17 return self.items.pop()1819 def peek(self):20 return self.items[len(self.items) - 1]2122 def size(self):23 return len(self.items)242526def BigTri(v):27 tri = []28 e = []29 x = v.getxy()[0]30 y = v.getxy()[1]31 tri.append(Vertex(x - 5000, y - 5000))32 tri.append(Vertex(x + 5000, y - 5000))33 tri.append(Vertex((x + x) / 2, y + 5000))34 e.append(MakeEdge(tri[0], tri[1]))35 e.append(MakeEdge(tri[1], tri[2]))36 e.append(MakeEdge(tri[2], tri[0]))37 Splice(e[0], e[2].Sym())38 Splice(e[1], e[0].Sym())39 Splice(e[2], e[1].Sym())40 return e414243def Circlecenter(tri):44 pts = np.asarray([v.getxy() for v in tri])45 pts2 = np.dot(pts, pts.T)46 A = np.bmat([[2 * pts2, [[1],47 [1],48 [1]]],49 [[[1, 1, 1, 0]]]])5051 b = np.hstack((np.sum(pts * pts, axis=1), [1]))52 x = np.linalg.solve(A, b)53 bary_coords = x[:-1]54 center = np.dot(bary_coords, pts)55 center[0] = round(center[0], 0)56 center[1] = round(center[1], 0)57 radius = np.sum(np.square(pts[0] - center)) # squared distance58 return (center, radius)596061def Quicksort(l):62 if len(l) <= 1: return l63 pivolt = l[random.randint(0, len(l) - 1)]64 small, eq, larger = [], [], []65 for i in l:66 dis = i67 if dis < pivolt:68 small.append(i)69 elif dis == pivolt:70 eq.append(i)71 else:72 larger.append(i)73 return Quicksort(small) + eq + Quicksort(larger)747576def InCircleFast(tri, p):77 """Check if point p is inside of precomputed circumcircle of tri.78 """79 center, radius = Circlecenter(tri)80 return np.sum(np.square(center - p.getxy())) <= radius818283def RemoveSplice(t1, t2, t3):84 Splice(t1, t2)85 Splice(t3.Sym(), t2.Sym())86 Splice(t1.Sym(), t2)878889def NewEdges(x, p):90 e = []91 e.append(MakeEdge(x.V, p))92 e.append(MakeEdge(x.NextV().Sym().V, p))93 e.append(MakeEdge(x.N.S.N.S.V, p))94 return e959697def Newconection(x, e):98 temp = x.NextV()99 Splice(x, temp)100 Splice(x, e)101 Splice(e, temp)102103104def SplitingTheTri(x, p):105 e = NewEdges(x, p)106 t = x.N.S107 t1 = x.N.S.N.S108 '''109 print("=-----")110 print("MAIN EDGE: ", x.V.Vertex_id, x.S.V.Vertex_id)111 print("SPLITING THE TRIANGLE")112113 print(x.V.Vertex_id, x.S.V.Vertex_id, "||", e[0].V.Vertex_id, e[0].S.V.Vertex_id)114 print(t1.V.Vertex_id, t1.S.V.Vertex_id, "||", e[2].V.Vertex_id, e[2].S.V.Vertex_id)115 print(t.V.Vertex_id, t.S.V.Vertex_id, "||", e[1].V.Vertex_id, e[1].S.V.Vertex_id)116 print(e[1].S.V.Vertex_id, e[1].S.S.V.Vertex_id, "||", e[0].S.V.Vertex_id, e[0].S.S.V.Vertex_id)117 print(e[2].S.V.Vertex_id, e[0].S.S.V.Vertex_id, "||", e[2].S.V.Vertex_id, e[2].S.S.V.Vertex_id)118 print("--------")119 '''120 Newconection(x, e[0])121 Newconection(t1, e[2])122 Newconection(t, e[1])123 Splice(e[1].S, e[0].S)124 Splice(e[0].S, e[2].S)125 return e126127128def Flip(x, p):129 for i in neighbours(x):130 if i.V.Vertex_id == p.Vertex_id:131 temp = x.N132 temp1 = x.N.S.N.S133 temp2 = x.S.N134 temp3 = x.S.N.S.N.S135 Splice(temp3, x)136 # Splice(x, temp)137 Splice(temp.S, temp1.S)138 Splice(temp1, x.S)139 # Splice(x.S, temp2)140 Splice(temp2.S, temp3.S)141 del x142143 e = MakeEdge(temp.S.V, temp3.S.V)144 Splice(temp2.S, e.S)145 Splice(e.S, temp3.S)146 Splice(temp.S, e)147 Splice(e, temp1.S)148149 return e, temp2.S, temp3150 else:151 pass152153154def R(p, p1, p2):155 px = p.getxy()[0]156 py = p.getxy()[1]157 p1x = p1.V.getxy()[0]158 p1y = p1.V.getxy()[1]159 p2x = p2.V.getxy()[0]160 p2y = p2.V.getxy()[1]161 x = np.linalg.det([[px, py, 1],162 [p1x, p1y, 1],163 [p2x, p2y, 1]])164 return x165166167def CCW(p, p1, p2):168 px = p.V.getxy()[0]169 py = p.V.getxy()[1]170 p1x = p1.V.getxy()[0]171 p1y = p1.V.getxy()[1]172 p2x = p2.V.getxy()[0]173 p2y = p2.V.getxy()[1]174 x = np.linalg.det([[px, py, 1],175 [p1x, p1y, 1],176 [p2x, p2y, 1]])177 return x178179180def Point_inside_tri(e, p):181 if int(R(p, e.S, e.N.S)) >= 0 and int(R(p, e.N.S, e)) >= 0 and int(R(p, e, e.S)) >= 0:182 return e183 else:184 return e.S185186187def WalkingInTri(e, p, con):188 while True:189 if int(R(p, e.S, e.N.S)) >= 0 and int(R(p, e.N.S, e)) >= 0 and int(R(p, e, e.S)) >= 0:190 if p.Vertex_id >= 2 and e.N.S.V.getxy() in con and e.V.getxy() in con and e.S.V.getxy() in con:191 e = e.N192 else:193 return e.N.S, e, e.N.S.N.S194 elif int(R(p, e.S, e.N.S)) <= 0 and int(R(p, e.N.S, e)) <= 0 and int(R(p, e, e.S)) <= 0:195 e = e.S196 elif R(p, e.S, e.N.S) < 0:197 e = e.N.S.N198 elif R(p, e.N.S, e) < 0:199 e = e.N200 elif R(p, e, e.S) < 0:201 e = e.S202203204def Delauny(point, edge):205 # print(" VERTEX NR:", point.Vertex_id)206 bad_triangles = WalkingInTri(edge[-1], point, tri)207 stack = Stack()208 for i in bad_triangles:209 stack.push(i)210 del edge211 edge = []212 s = SplitingTheTri(bad_triangles[0], point)213 for k in s:214 edge.append(k)215 while stack.size() != 0:216 temp = stack.pop()217 # print(temp.V.Vertex_id, temp.S.V.Vertex_id)218 # print(temp.V.Vertex_id, point.Vertex_id, temp.Sym().V.Vertex_id,219 # Point_inside_tri(temp, point).S.N.S.V.Vertex_id)220 if InCircleFast([temp.V, point, temp.Sym().V], Point_inside_tri(temp, point).S.N.S.V):221 f = Flip(temp, point)222 # print("PUSH")223 # print(f[1].V.Vertex_id, f[1].S.V.Vertex_id)224 # print(f[2].V.Vertex_id, f[2].S.V.Vertex_id)225 del temp226227 stack.push(f[1])228 stack.push(f[2])229 edge.append(f[0])230231 else:232 pass233 return edge234235236def ReturnVertex(v):237 return v238239240def DrawDT(drawing_points):241 t._tracer(0)242 # Function for drawing the whole DT243 for vert in drawing_points:244 for i in neighbours(vert):245 turtle.penup()246 turtle.goto(i.V.getxy()[0] + 15, i.V.getxy()[1])247 turtle.write(i.V.Vertex_id)248 turtle.goto(i.V.getxy())249 turtle.pendown()250 turtle.goto(i.Sym().V.getxy())251 turtle.penup()252 turtle.goto(i.Sym().V.getxy()[0] + 15, i.Sym().V.getxy()[1])253 turtle.write(i.Sym().V.Vertex_id)254255256def DeletingLine():257 global drawing_edge258 global taken_edge259 Turtle_drawingline(taken_edge, "white", 5)260 Turtle_drawingline(taken_edge, "black", 1)261 taken_edge = None262263264def Turtle_drawingline(edge, color, width):265 t.penup()266 t.pensize(width)267 t.pencolor(color)268 t.goto(edge.V.getxy())269 t.pendown()270 t.goto(edge.S.V.getxy())271 t.penup()272 t.pensize(1)273274275def DrawingLine():276 global drawing_edge277 global taken_edge278 if taken_edge != None:279 DeletingLine()280 taken_edge = drawing_edge281 drawing_edge = None282 Turtle_drawingline(taken_edge, "brown", 5)283284285def NextEdge():286 global taken_edge287 global drawing_edge288 if taken_edge == None:289 pass290 drawing_edge = taken_edge.N291 DrawingLine()292293294def PrevEdge():295 global taken_edge296 global drawing_edge297 if taken_edge == None:298 pass299 drawing_edge = taken_edge.S.N.S.N.S300 DrawingLine()301302303def SymEdge():304 global taken_edge305 global drawing_edge306 if taken_edge == None:307 pass308 drawing_edge =taken_edge.S309 DrawingLine()310def ExitScrean():311 turtle.bye()312313314def distance(point, coef):315 return abs((coef[0] * point[0]) - point[1] + coef[1]) / math.sqrt((coef[0] * coef[0]) + 1)316317318def DistanceFrom(e, point):319 distance = np.linalg.norm(np.cross(np.array(e[0].S.V.getxy()) - np.array(e[0].V.getxy()),320 np.array(e[0].V.getxy()) - np.array(point.getxy()))) / np.linalg.norm(321 np.array(e[0].S.V.getxy()) - np.array(e[0].V.getxy()))322 distance1 = np.linalg.norm(np.cross(np.array(e[1].S.V.getxy()) - np.array(e[1].V.getxy()),323 np.array(e[1].V.getxy()) - np.array(point.getxy()))) / np.linalg.norm(324 np.array(e[1].S.V.getxy()) - np.array(e[1].V.getxy()))325 distance2 = np.linalg.norm(np.cross(np.array(e[2].S.V.getxy()) - np.array(e[2].V.getxy()),326 np.array(e[2].V.getxy()) - np.array(point.getxy()))) / np.linalg.norm(327 np.array(e[2].S.V.getxy()) - np.array(e[2].V.getxy()))328329 if distance < distance1 and distance < distance2:330 return e[0]331 elif distance1 < distance and distance1 < distance2:332 return e[1]333 elif distance2 < distance1 and distance2 < distance:334 return e[2]335336337def ClickOnEdge(x, y):338 # Findning the nearest line to the position where you click339 global drawing_edge340 global taken_edge341 global main_edge342 point = Vertex(x, y)343 if taken_edge != None:344 turtle.resetscreen()345 remove_teken(edges)346 remove_visited(vertex)347 drawing_points = bfs(taken_edge)348 DrawDT(drawing_points)349 taken_edge = None350 searching_tri = WalkingInTri(main_edge, point, tri)351 drawing_edge = DistanceFrom(searching_tri, point)352 DrawingLine()353354355356def AddPoint(x, y):357 global edges358 global vertex359 global taken_edge360 vertex.append(Vertex(x, y))361362 et = Delauny(vertex[-1], edges)363 for ez in et:364 edges.append(ez)365 turtle.resetscreen()366 remove_teken(edges)367 remove_visited(vertex)368 drawing_points = bfs(edges[-1])369 DrawDT(drawing_points)370371372if __name__ == '__main__':373 global drawing_edge374 global taken_edge375 global main_edge376 global drawing_points377 global vertex378 global edges379 vertex = []380 vertex.append(Vertex(20, 50))381 vertex.append(Vertex(120, 80))382 vertex.append(Vertex(100, 220))383 vertex.append(Vertex(300, 250))384 vertex.append(Vertex(200, 25))385386 edges = []387 global tri388 tri = BigTri(vertex[0])389390 for edg in tri:391 edges.append(edg)392 # print(R(vertex[0], edges[0], edges[0].Sym()))393 for element in range(len(tri)):394 tri[element] = tri[element].V.getxy()395 for j in vertex:396 et = Delauny(j, edges)397 for ez in et:398 edges.append(ez)399 drawing_points = bfs(edges[-1])400 print(len(drawing_points))401 turtle.tracer(0)402403404 main_edge = edges[0]405 drawing_edge = None406 taken_edge = None407 t = turtle.Turtle()408 t.speed(0)409 DrawDT(drawing_points)410411 turtle.onkey(NextEdge, 'w')412413 turtle.onkey(PrevEdge, 'e')414415 turtle.onkey(SymEdge, 's')416417 turtle.onkey(ExitScrean, 'q')418419 turtle.onscreenclick(ClickOnEdge, 1)420421 turtle.onscreenclick(AddPoint, 3)422423 turtle.listen()424425 turtle.mainloop()
...
Turtle_drawing.py
Source:Turtle_drawing.py
1import turtle2from turtle import *34from HALF_EDGE.Delauny_Triangulation import *5from HALF_EDGE.Half_Edge_DS import *6from HALF_EDGE.half_edge import *7import numpy as np8910def DrawDT(drawing_points):11 # Function for drawing the whole DT12 for vert in drawing_points:13 for i in neighbours(vert):14 turtle.penup()15 turtle.goto(i.V.getxy()[0] + 15, i.V.getxy()[1])16 turtle.write(i.V.Vertex_id)17 turtle.goto(i.V.getxy())18 turtle.pendown()19 turtle.goto(i.Sym().V.getxy())20 turtle.penup()21 turtle.goto(i.Sym().V.getxy()[0] + 15, i.Sym().V.getxy()[1])22 turtle.write(i.Sym().V.Vertex_id)2324 pass252627def DeletingLine():28 global drawing_edge29 global taken_edge30 Turtle_drawingline(taken_edge, "white", 5)31 taken_edge = None323334def Turtle_drawingline(edge, color, width):35 t.penup()36 t.pensize(width)37 t.pencolor(color)38 t.goto(edge.V.getxy())39 t.pendown()40 t.goto(edge.S.V.getxy())41 t.penup()42 t.pensize(1)434445def DrawingLine():46 global drawing_edge47 global taken_edge48 if taken_edge != None:49 DeletingLine(taken_edge)50 taken_edge = drawing_edge51 drawing_edge = None52 Turtle_drawingline(taken_edge, "brown", 5)535455def NextEdge():56 t.setheading(90)57 t.forward(100)585960def PrevEdge():61 pass626364656667def SymEdge():68 pass697071def ExitScrean():72 turtle.bye()737475def DistanceFrom(e, point):76 x1, y1 = ((e[0].V.getxy()[0] + e[0].S.V.getxy()[0]) // 2), ((e[0].V.getxy()[1] + e[1].S.V.getxy()[1]) // 2)77 x2, y2 = ((e[1].V.getxy()[0] + e[1].S.V.getxy()[0]) // 2), ((e[1].V.getxy()[1] + e[1].S.V.getxy()[1]) // 2)78 x3, y3 = ((e[2].V.getxy()[0] + e[2].S.V.getxy()[0]) // 2), ((e[2].V.getxy()[1] + e[2].S.V.getxy()[1]) // 2)79 distance = math.sqrt((x1 - point.getxy()[0]) ** 2, (y1 - point.getxy()[1]) ** 2)80 distance1 = math.sqrt((x2 - point.getxy()[0]) ** 2, (y2 - point.getxy()[1]) ** 2)81 distance2 = math.sqrt((x3 - point.getxy()[0]) ** 2, (y3 - point.getxy()[1]) ** 2)82 if distance < distance1 and distance < distance2:83 return e[0]84 elif distanc1 < distance and distance1 < distance2:85 return e[1]86 elif distance2 < distance1 and distance2 < distance0:87 return e[2]8889def WalkingInTri(e, p, con):90 while True:91 if int(R(p, e.S, e.N.S)) >= 0 and int(R(p, e.N.S, e)) >= 0 and int(R(p, e, e.S)) >= 0:92 if p.Vertex_id >= 2 and e.N.S.V.getxy() in con and e.V.getxy() in con and e.S.V.getxy() in con:93 e = e.N94 else:95 return e.N.S, e, e.N.S.N.S96 elif int(R(p, e.S, e.N.S)) <= 0 and int(R(p, e.N.S, e)) <= 0 and int(R(p, e, e.S)) <= 0:97 e = e.S98 elif R(p, e.S, e.N.S) < 0:99 e = e.N.S.N100 elif R(p, e.N.S, e) < 0:101 e = e.N102 elif R(p, e, e.S) < 0:103 e = e.S104105def ClickOnEdge(x, y):106 # Findning the nearest line to the position where you click107 global drawing_edge108 global taken_edge109 global main_edge110 point = Vertex(x, y)111 searching_tri=WalkingInTri(main_edge, point, t1)112 drawing_edge = DistanceFrom(searching_tri, point)113 DrawingLine()114115 pass116117118def AddPoint(x, y):119 pass120121122def Graphicaluserinterface(e,drawing_points,t):123 global drawing_edge124 global taken_edge125 global main_edge126 global t1127 t1=t128 main_edge=e129 drawing_edge = None130 taken_edge = None131 t = turtle.Turtle()132 t.speed(0)133 DrawDT(drawing_points)134135 turtle.onkey(NextEdge, 'Up')136137 turtle.onkey(PrevEdge, 'Up')138139 turtle.onkey(SymEdge, 'Up')140141 turtle.onkey(ExitScrean, 'q')142143 turtle.onscreenclick(ClickOnEdge, 1)144145 turtle.onscreenclick(AddPoint, 3)146147 turtle.listen()148
...
genPics.py
Source:genPics.py
...17 path = parent.simplePath.points18 for point in range(len(path)-1):19 start = path[point]20 next = path[point+1]21 draw.line((start.getXY(), next.getXY()), fill=COLOR_SIMPLE)22 drawSimple.line((start.getXY(), next.getXY()), fill=COLOR_SIMPLE)23 path = parent.smoothPath.points24 for point in range(len(path)-1):25 start = path[point]26 next = path[point+1]27 draw.line((start.getXY(), next.getXY()), fill=COLOR_SMOOTH)28 drawSmooth.line((start.getXY(), next.getXY()), fill=COLOR_SMOOTH)29 del draw30 del drawSmooth31 del drawSimple32 return (image, simpleImage, smoothImage)33 """34 draw = ImageDraw.Draw(image)35 draw.line((0, 0) + image.size, fill=128)36 draw.line((0, image.size[1], image.size[0], 0), fill=(0xff,0xff,0xff,0xff))37 del draw38 """39if __name__ == "__main__":40 for i in range(12):41 image = Image.open("paths/trialPNG(%d).png" % (i))42 with open("paths/trialFRAME(%d).txt" % (i), "r") as f:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!