Rhinoscript, Test a point if is in or out of triangle

This code test if a point is inside of a triangle or not. The code prints the result in the prompt line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Option Explicit
'Script version miƩrcoles, 11 de marzo de 2009 20:04:25

Call Main()
Sub Main()
 
	'dim 3 points
	Dim point1 : point1 = rhino.GetPoint("select point 1")
	Dim point2 : point2 = rhino.GetPoint("select point 2")
	Dim point3 : point3 = rhino.GetPoint("select point 3")
 
	Dim P : P = rhino.GetPoint("select point")
 
	rhino.AddPoint P
	Dim t: t = rhino.AddPolyline(array(point1, point2, point3, point1))
 
	Call PtInTriangle(point1, point2, point3, p)
 
End Sub
 
Function PtInTriangle(A1, A2, A3, p)
 
	Dim Orient 'orientation of the triangle
	'(A1.x - A3.x) * (A2.y - A3.y) - (A1.y - A3.y) * (A2.x - A3.x)
	Orient = (A1(0) - A3(0)) * (A2(1) - A3(1)) - (A1(1) - A3(1)) * (A2(0) - A3(0))
 
	Call Rhino.Print(orient)
 
	'find if the point is inside or not of the triangle.
	Dim O1, O2, O3 'A1A2P, A2A3P, A3A1P
	O1 = (A1(0) - p(0)) * (A2(1) - p(1)) - (A1(1) - p(1)) * (A2(0) - p(0))
	O2 = (A2(0) - p(0)) * (A3(1) - p(1)) - (A2(1) - p(1)) * (A3(0) - p(0))
	O3 = (A3(0) - p(0)) * (A1(1) - p(1)) - (A3(1) - p(1)) * (A1(0) - p(0))
 
	If Orient <= 0 And O1 <= 0 And O2 <= 0 And O3 <= 0 Then 		Call Rhino.Print("inside") 	ElseIf Orient >= 0 And O1 >= 0 And O2 >= 0 And O3 >= 0 Then
		Call Rhino.Print("inside")
 
	Else
		Call Rhino.Print("out")
 
	End If
 
End Function

Leave a Reply