ผลต่างระหว่างรุ่นของ "เกรแฮมสแกน"

เนื้อหาที่ลบ เนื้อหาที่เพิ่ม
Djnoly (คุย | ส่วนร่วม)
ไม่มีความย่อการแก้ไข
Djnoly (คุย | ส่วนร่วม)
บรรทัด 19:
==รหัสเทียม==
อันดับแรก ฟังก์ชั่นคำนวนการ "เลี้ยวขวา" และ "เลี้ยวซ้าย"
''จุดสามจุดก่อให้เกิดการ "เลี้ยวซ้าย" เมื่อ ccw > 0, เล"เลี้ยวขวา" เมื่อ ccw < 0 และ เรียงตัวเป็นเส้นตรงเมื่อ ccw = 0 เนื่องจาก ccw คือพื้นที่สามเหลี่ยมจากการวางตัวของจุด p1, p2 และ p3 โดยคิดเครื่องหมายบวก ลบ ด้วย'''
# ''ccw < 0, and collinear if ccw = 0 because ccw is a determinant that''
'''ฟังก์ชั่น''' ccw(p1, p2, p3):
# ''gives the signed area of the triangle formed by p1, p2 and p3.''
'''returnคืนค่า''' (p2p2.x - p1p1.x)*(p3p3.y - p1p1.y) - (p2p2.y - p1p1.y)*(p3p3.x - p1p1.x)
'''function''' ccw(p1, p2, p3):
'''return''' (p2.x - p1.x)*(p3.y - p1.y) - (p2.y - p1.y)*(p3.x - p1.x)
 
และให้คำตอบอยู่ในตาราง <code> points </code>
 
'''ให้''' N = number of pointsจำนวนจุดทั้งหมด
'''ให้''' points[N+1] = the array of pointsตารางของจุดทั้งหมด
'''สลับ''' points[1] withกับจุดที่มีตำแหน่งบนแกน they point with the lowest y-coordinateต่าที่สุด
'''เรียง''' points by polar angle withจากมุมที่จุดต่างๆกระทำกับจุด points[1] จากน้อยไปหามาก
# ''ต้องการให้ points[0] เป็นจุดสุดท้ายซึ่งจะจบการทำงานในวงวน ดังนั้น''
# ''We want points[0] to be a sentinel point that will stop the loop.''
'''letให้''' points[0] = points[N]
# ''ค่า M will denote the number of points on theจะแสดงถึงจำนวนจุดบนผนัง convex hull.''
'''letให้''' M = 2
'''for''' i = 3 '''to''' N:
# ''Find next valid point onหาจุดบนผนัง convex hull. จุดถัดไป''
'''while''' '''ccw'''(points[M-1], points[M], points[i]) <= 0:
# ''หากจุดทั้งสามเรียงตัวกันเป็นเส้นตรง จะไม่สนใจจุดนั้น''
# ''Check if first points are collinear, if so ignore unnecessary points.''
'''if''' M is== 2:
'''swapสลับ''' points[M] withกับ points[i]
i += 1
'''else'''
M -= 1
# ''เพิ่มค่า M ให้ถูกต้อง และสลับจุด points[i] มายังส่วนคำตอบ''
# ''Update M and swap points[i] to the correct place.''
M += 1
'''swapสลับ''' points[M] withกับ points[i]
 
รหัสเทียมนี้ปรับปรุงจากตำรา ''Algorithms, 4th edition'' โดย Sedgewick and Wayne