Checking the point if internal among three points with python

10478 단어 Python
import matplotlib.pyplot as plt
import numpy as np
import sys
import random

pa=[random.randint(-10,10),random.randint(-10,10)]
pb=[random.randint(-10,10),random.randint(-10,10)]
pc=[random.randint(-10,10),random.randint(-10,10)]
x=float(sys.argv[1])
y=float(sys.argv[2])

def contains(g, p): 
  n = len(g)
  x = False
  for i in range(n):
    a = np.array(g[i]) - np.array(p)
    b = g[(i+1)%n] - p 
    if (np.linalg.norm(np.cross(a,b)) < np.finfo(np.float).eps \
        and np.dot(a,b) < np.finfo(np.float).eps):
        return 1
    if (a[1] > b[1]):
        b,a = a,b 
    if (a[1] < np.finfo(np.float).eps and np.finfo(np.float).eps < b[1] \
        and np.cross(a,b) > np.finfo(np.float).eps):
        x = not(x)
  if (x):
      return 2
  return 0

print(pa,pb,pc)
print(contains(np.array([pa,pb,pc]),np.array([x,y])))
left = np.array([pa[0],pb[0],pc[0],pa[0]])
height = np.array([pa[1],pb[1],pc[1],pa[1]])
plt.plot(left, height)
plt.plot(x,y,'bo')
plt.show()
  • example

  • Refs.


    https://qiita.com/adad/items/b6842af8778ddff10d77
    http://www.geisya.or.jp/~mwm48961/koukou/area1.htm
    https://note.nkmk.me/python-random-randrange-randint/

    좋은 웹페이지 즐겨찾기