LoginSignup
0
0

More than 5 years have passed since last update.

Checking the point if internal among three points with python

Last updated at Posted at 2018-08-05
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 スクリーンショット 2018-08-06 17.32.19.png

Refs.

https://qiita.com/adad/items/b6842af8778ddff10d77

http://www.geisya.or.jp/~mwm48961/koukou/area1.htm

https://note.nkmk.me/python-random-randrange-randint/

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0