-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_bezier.py
161 lines (146 loc) · 23 KB
/
test_bezier.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# import cv2
# import numpy as np
#
#
# def cubic_bezier_sum(t, w):
# t2 = t * t
# t3 = t2 * t
# mt = 1 - t
# mt2 = mt * mt
# mt3 = mt2 * mt
#
# return w[0] * mt3 + 3 * w[1] * mt2 * t + 3 * w[2] * mt * t2 + w[3] * t3
#
#
# def draw_cubic_bezier(p1, p2, p3, p4):
# points = []
# t = 0
# while t < 1:
# x = int(cubic_bezier_sum(t, (p1[0], p2[0], p3[0], p4[0])))
# y = int(cubic_bezier_sum(t, (p1[1], p2[1], p3[1], p4[1])))
#
# points.append((x, y))
#
# t += 0.01
# return points
#
# points = []
# points.append((635, 489))
# points.append((612, 489))
# points.append((540, 486))
# points.append((519, 476))
#
# bezier_points = draw_cubic_bezier(points[0], points[1], points[2], points[3])
#
# print(len(bezier_points))
#
# img_path = "../templates/stroke_dan.png"
#
# img = cv2.imread(img_path, 0)
# _, img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
# img_rgb = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
#
# for id in range(len(bezier_points)-1):
# start_pt = bezier_points[id]
# end_pt = bezier_points[id+1]
# cv2.line(img_rgb, start_pt, end_pt, (0, 255, 0))
#
# cv2.imshow("rgb", img_rgb)
#
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# import numpy as np
# from scipy.misc import comb
#
# def bernstein_poly(i, n, t):
# """
# The Bernstein polynomial of n, i as a function of t
# """
#
# return comb(n, i) * ( t**(n-i) ) * (1 - t)**i
#
#
# def bezier_curve(points, nTimes=100):
# """
# Given a set of control points, return the
# bezier curve defined by the control points.
#
# points should be a list of lists, or list of tuples
# such as [ [1,1],
# [2,3],
# [4,5], ..[Xn, Yn] ]
# nTimes is the number of time steps, defaults to 1000
#
# See http://processingjs.nihongoresources.com/bezierinfo/
# """
#
# nPoints = len(points)
# xPoints = np.array([p[0] for p in points])
# yPoints = np.array([p[1] for p in points])
#
# t = np.linspace(0.0, 1.0, nTimes)
#
# polynomial_array = np.array([ bernstein_poly(i, nPoints-1, t) for i in range(0, nPoints) ])
#
# xvals = np.dot(xPoints, polynomial_array)
# yvals = np.dot(yPoints, polynomial_array)
#
# return xvals, yvals
#
#
# if __name__ == "__main__":
# from matplotlib import pyplot as plt
#
# # nPoints = 4
# # points = np.random.rand(nPoints,2)*200
#
# points = [(646, 737), (647, 737), (648, 738), (648, 739), (648, 740), (648, 741), (648, 742), (648, 743), (648, 744), (648, 745), (648, 746), (648, 747), (648, 748), (648, 749), (648, 750), (647, 750), (646, 750), (645, 750), (644, 751), (643, 752), (643, 753), (643, 754), (643, 755), (643, 756), (643, 757), (643, 758), (643, 759), (643, 760), (643, 761), (643, 762), (643, 763), (643, 764), (643, 765), (643, 766), (643, 767), (643, 768), (643, 769), (643, 770), (642, 770), (641, 770), (640, 771), (639, 772), (639, 773), (639, 774), (639, 775), (639, 776), (639, 777), (639, 778), (639, 779), (638, 779), (637, 779), (636, 780), (635, 781), (635, 782), (635, 783), (635, 784), (635, 785), (635, 786), (635, 787), (635, 788), (635, 789), (635, 790), (635, 791), (635, 792), (635, 793), (635, 794), (635, 795), (635, 796), (635, 797), (635, 798), (635, 799), (635, 800), (634, 800), (633, 800), (632, 801), (631, 802), (631, 803), (631, 804), (631, 805), (631, 806), (631, 807), (631, 808), (630, 808), (629, 808), (628, 808), (627, 808), (626, 808), (625, 808), (624, 809), (623, 810), (623, 811), (623, 812), (622, 812), (621, 812), (620, 812), (619, 812), (618, 812), (617, 812), (616, 812), (615, 813), (614, 814), (614, 815), (614, 816), (613, 816), (612, 816), (611, 817), (610, 818), (610, 819), (610, 820), (609, 820), (608, 820), (607, 821), (606, 822), (606, 823), (606, 824), (606, 825), (605, 825), (604, 825), (603, 826), (602, 827), (602, 828), (602, 829), (602, 830), (602, 831), (602, 832), (602, 833), (601, 833), (600, 833), (599, 834), (598, 835), (598, 836), (598, 837), (597, 837), (596, 837), (595, 837), (594, 838), (593, 839), (593, 840), (593, 841), (592, 841), (591, 841), (590, 842), (589, 843), (589, 844), (589, 845), (589, 846), (589, 847), (589, 848), (589, 849), (589, 850), (589, 851), (589, 852), (589, 853), (589, 854), (589, 855), (589, 856), (589, 857), (589, 858), (588, 858), (587, 858), (586, 859), (585, 860), (585, 861), (585, 862), (585, 863), (585, 864), (585, 865), (585, 866), (584, 866), (583, 866), (582, 866), (581, 866), (580, 866), (579, 866), (578, 867), (577, 868), (577, 869), (577, 870), (577, 871), (577, 872), (577, 873), (577, 874), (577, 875), (577, 876), (577, 877), (577, 878), (577, 879), (577, 880), (577, 881), (577, 882), (577, 883), (576, 883), (575, 883), (574, 884), (573, 885), (573, 886), (573, 887), (572, 887), (571, 887), (570, 887), (569, 888), (568, 889), (568, 890), (568, 891), (567, 891), (566, 891), (565, 892), (564, 893), (564, 894), (564, 895), (564, 896), (564, 897), (564, 898), (564, 899), (564, 900), (564, 901), (564, 902), (564, 903), (564, 904), (563, 904), (562, 904), (561, 905), (560, 906), (560, 907), (560, 908), (559, 908), (558, 908), (557, 909), (556, 910), (556, 911), (556, 912), (555, 912), (554, 912), (553, 913), (552, 914), (552, 915), (552, 916), (552, 917), (552, 918), (552, 919), (553, 920), (554, 920), (555, 920), (556, 921), (556, 922), (556, 923), (556, 924), (556, 925), (556, 926), (556, 927), (556, 928), (556, 929), (555, 929), (554, 929), (553, 930), (552, 931), (552, 932), (552, 933), (551, 933), (550, 933), (549, 934), (548, 935), (548, 936), (548, 937), (547, 937), (546, 937), (545, 937), (544, 937), (543, 937), (542, 937), (541, 937), (540, 938), (539, 939), (539, 940), (539, 941), (538, 941), (537, 941), (536, 942), (535, 943), (535, 944), (535, 945), (534, 945), (533, 945), (532, 946), (531, 947), (531, 948), (531, 949), (531, 950), (530, 950), (529, 950), (528, 951), (527, 952), (527, 953), (527, 954), (527, 955), (527, 956), (527, 957), (527, 958), (526, 958), (525, 958), (524, 959), (523, 960), (523, 961), (523, 962), (522, 962), (521, 962), (520, 962), (519, 962), (518, 962), (517, 962), (516, 962), (515, 963), (514, 964), (514, 965), (514, 966), (513, 966), (512, 966), (511, 967), (510, 968), (510, 969), (510, 970), (509, 970), (508, 970), (507, 971), (506, 972), (506, 973), (506, 974), (506, 975), (506, 976), (506, 977), (506, 978), (506, 979), (505, 979), (504, 979), (503, 979), (502, 979), (501, 979), (500, 979), (499, 980), (498, 981), (498, 982), (498, 983), (497, 983), (496, 983), (495, 983), (494, 984), (493, 985), (493, 986), (493, 987), (492, 987), (491, 987), (490, 987), (489, 987), (488, 987), (487, 987), (486, 988), (485, 989), (485, 990), (485, 991), (485, 992), (485, 993), (485, 994), (485, 995), (485, 996), (485, 997), (485, 998), (485, 999), (485, 1000), (485, 1001), (485, 1002), (485, 1003), (485, 1004), (484, 1004), (483, 1004), (482, 1004), (481, 1004), (480, 1004), (479, 1004), (478, 1005), (477, 1006), (477, 1007), (477, 1008), (476, 1008), (475, 1008), (474, 1008), (473, 1008), (472, 1008), (471, 1008), (470, 1008), (469, 1009), (468, 1010), (468, 1011), (468, 1012), (467, 1012), (466, 1012), (465, 1012), (464, 1012), (463, 1012), (462, 1012), (461, 1013), (460, 1014), (460, 1015), (460, 1016), (459, 1016), (458, 1016), (457, 1016), (456, 1016), (455, 1016), (454, 1016), (453, 1016), (452, 1016), (451, 1016), (450, 1016), (449, 1017), (448, 1018), (448, 1019), (448, 1020), (447, 1020), (446, 1020), (445, 1020), (444, 1021), (443, 1022), (443, 1023), (443, 1024), (443, 1025), (443, 1026), (443, 1027), (443, 1028), (443, 1029), (442, 1029), (441, 1029), (440, 1030), (439, 1031), (439, 1032), (439, 1033), (438, 1033), (437, 1033), (436, 1034), (435, 1035), (435, 1036), (435, 1037), (434, 1037), (433, 1037), (432, 1037), (431, 1037), (430, 1037), (429, 1037), (428, 1038), (427, 1039), (427, 1040), (427, 1041), (426, 1041), (425, 1041), (424, 1041), (423, 1041), (422, 1041), (421, 1041), (420, 1041), (419, 1042), (418, 1043), (418, 1044), (418, 1045), (417, 1045), (416, 1045), (415, 1046), (414, 1047), (414, 1048), (414, 1049), (414, 1050), (413, 1050), (412, 1050), (411, 1051), (410, 1052), (410, 1053), (410, 1054), (409, 1054), (408, 1054), (407, 1055), (406, 1056), (406, 1057), (406, 1058), (405, 1058), (404, 1058), (403, 1059), (402, 1060), (402, 1061), (402, 1062), (401, 1062), (400, 1062), (399, 1062), (398, 1062), (397, 1062), (396, 1062), (395, 1062), (394, 1063), (393, 1064), (393, 1065), (393, 1066), (392, 1066), (391, 1066), (390, 1067), (389, 1068), (389, 1069), (389, 1070), (388, 1070), (387, 1070), (386, 1071), (385, 1072), (385, 1073), (385, 1074), (385, 1075), (384, 1075), (383, 1075), (382, 1076), (381, 1077), (381, 1078), (381, 1079), (380, 1079), (379, 1079), (378, 1079), (377, 1079), (376, 1079), (375, 1079), (374, 1080), (373, 1081), (373, 1082), (373, 1083), (372, 1083), (371, 1083), (370, 1083), (369, 1083), (368, 1083), (367, 1083), (366, 1083), (365, 1083), (364, 1083), (363, 1083), (362, 1083), (361, 1084), (360, 1085), (360, 1086), (360, 1087), (359, 1087), (358, 1087), (357, 1087), (356, 1087), (355, 1087), (354, 1087), (353, 1088), (352, 1089), (352, 1090), (352, 1091), (351, 1091), (350, 1091), (349, 1091), (348, 1091), (347, 1091), (346, 1091), (345, 1091), (344, 1092), (343, 1093), (343, 1094), (343, 1095), (342, 1095), (341, 1095), (340, 1095), (339, 1095), (338, 1095), (337, 1095), (336, 1096), (335, 1097), (335, 1098), (335, 1099), (335, 1100), (334, 1100), (333, 1100), (332, 1100), (331, 1100), (330, 1100), (329, 1100), (328, 1100), (327, 1100), (326, 1100), (325, 1100), (324, 1101), (323, 1102), (323, 1103), (323, 1104), (322, 1104), (321, 1104), (320, 1104), (319, 1104), (318, 1104), (317, 1104), (316, 1104), (315, 1104), (314, 1104), (313, 1104), (312, 1104), (311, 1105), (310, 1106), (310, 1107), (310, 1108), (309, 1108), (308, 1108), (307, 1108), (306, 1108), (305, 1108), (304, 1108), (303, 1108), (302, 1108), (301, 1108), (300, 1108), (299, 1108), (298, 1108), (297, 1108), (296, 1108), (295, 1108), (294, 1109), (293, 1110), (293, 1111), (293, 1112), (292, 1112), (291, 1112), (290, 1112), (289, 1112), (288, 1112), (287, 1112), (286, 1112), (285, 1112), (284, 1112), (283, 1112), (282, 1112), (281, 1112), (280, 1112), (279, 1112), (278, 1112), (277, 1112), (276, 1112), (275, 1112), (274, 1112), (273, 1112), (272, 1112), (271, 1112), (270, 1112), (269, 1112), (268, 1112), (267, 1112), (266, 1112), (265, 1113), (264, 1114), (264, 1115), (264, 1116), (263, 1116), (262, 1116), (261, 1116), (260, 1116), (259, 1116), (258, 1116), (257, 1116), (256, 1116), (255, 1116), (254, 1116), (253, 1116), (252, 1116), (251, 1116), (250, 1116), (249, 1116), (248, 1116), (247, 1116), (246, 1116), (245, 1116), (244, 1116), (243, 1116), (242, 1116), (241, 1116), (240, 1116), (239, 1115), (239, 1114), (239, 1113), (238, 1112), (237, 1112), (236, 1112), (235, 1111), (235, 1110), (235, 1109), (234, 1108), (233, 1108), (232, 1108), (231, 1108), (230, 1108), (229, 1108), (228, 1108), (227, 1108), (226, 1108), (225, 1108), (224, 1109), (223, 1110), (223, 1111), (223, 1112), (222, 1112), (221, 1112), (220, 1112), (219, 1113), (218, 1114), (218, 1115), (218, 1116), (217, 1116), (216, 1116), (215, 1116), (214, 1116), (213, 1116), (212, 1116), (211, 1116), (210, 1116), (209, 1116), (208, 1116), (207, 1116), (206, 1116), (205, 1116), (204, 1116), (203, 1116), (202, 1115), (202, 1114), (202, 1113), (201, 1112), (200, 1112), (199, 1112), (198, 1111), (198, 1110), (198, 1109), (198, 1108), (198, 1107), (198, 1106), (198, 1105), (198, 1104), (198, 1103), (198, 1102), (198, 1101), (198, 1100), (198, 1099), (198, 1098), (198, 1097), (199, 1096), (200, 1095), (201, 1095), (202, 1095), (202, 1094), (202, 1093), (202, 1092), (202, 1091), (202, 1090), (202, 1089), (203, 1088), (204, 1087), (205, 1087), (206, 1087), (207, 1087), (208, 1087), (209, 1087), (210, 1087), (210, 1086), (210, 1085), (211, 1084), (212, 1083), (213, 1083), (214, 1083), (215, 1083), (216, 1083), (217, 1083), (218, 1083), (219, 1083), (220, 1083), (221, 1083), (222, 1083), (223, 1083), (224, 1083), (225, 1083), (226, 1083), (227, 1083), (228, 1083), (229, 1083), (230, 1083), (231, 1083), (231, 1082), (231, 1081), (232, 1080), (233, 1079), (234, 1079), (235, 1079), (235, 1078), (235, 1077), (236, 1076), (237, 1075), (238, 1075), (239, 1075), (240, 1075), (241, 1075), (242, 1075), (243, 1075), (244, 1075), (245, 1075), (246, 1075), (247, 1075), (248, 1075), (249, 1075), (250, 1075), (251, 1075), (252, 1075), (253, 1075), (254, 1075), (255, 1075), (256, 1075), (256, 1074), (256, 1073), (256, 1072), (257, 1071), (258, 1070), (259, 1070), (260, 1070), (261, 1070), (262, 1070), (263, 1070), (264, 1070), (265, 1070), (266, 1070), (267, 1070), (268, 1070), (268, 1069), (268, 1068), (268, 1067), (268, 1066), (268, 1065), (268, 1064), (268, 1063), (268, 1062), (268, 1061), (268, 1060), (268, 1059), (268, 1058), (268, 1057), (268, 1056), (268, 1055), (268, 1054), (268, 1053), (268, 1052), (269, 1051), (270, 1050), (271, 1050), (272, 1050), (273, 1050), (273, 1049), (273, 1048), (273, 1047), (273, 1046), (273, 1045), (273, 1044), (273, 1043), (274, 1042), (275, 1041), (276, 1041), (277, 1041), (278, 1041), (279, 1041), (280, 1041), (281, 1041), (281, 1040), (281, 1039), (282, 1038), (283, 1037), (284, 1037), (285, 1037), (285, 1036), (285, 1035), (285, 1034), (285, 1033), (285, 1032), (285, 1031), (286, 1030), (287, 1029), (288, 1029), (289, 1029), (289, 1028), (289, 1027), (290, 1026), (291, 1025), (292, 1025), (293, 1025), (294, 1025), (295, 1025), (296, 1025), (297, 1025), (298, 1025), (298, 1024), (298, 1023), (298, 1022), (299, 1021), (300, 1020), (301, 1020), (302, 1020), (302, 1019), (302, 1018), (303, 1017), (304, 1016), (305, 1016), (306, 1016), (306, 1015), (306, 1014), (306, 1013), (306, 1012), (306, 1011), (306, 1010), (307, 1009), (308, 1008), (309, 1008), (310, 1008), (311, 1008), (312, 1008), (313, 1008), (314, 1008), (314, 1007), (314, 1006), (315, 1005), (316, 1004), (317, 1004), (318, 1004), (319, 1004), (320, 1004), (321, 1004), (322, 1004), (323, 1004), (324, 1004), (325, 1004), (326, 1004), (327, 1004), (327, 1003), (327, 1002), (328, 1001), (329, 1000), (330, 1000), (331, 1000), (331, 999), (331, 998), (331, 997), (332, 996), (333, 995), (334, 995), (335, 995), (335, 994), (335, 993), (336, 992), (337, 991), (338, 991), (339, 991), (340, 991), (341, 991), (342, 991), (343, 991), (343, 990), (343, 989), (344, 988), (345, 987), (346, 987), (347, 987), (348, 987), (348, 986), (348, 985), (349, 984), (350, 983), (351, 983), (352, 983), (352, 982), (352, 981), (353, 980), (354, 979), (355, 979), (356, 979), (356, 978), (356, 977), (357, 976), (358, 975), (359, 975), (360, 975), (361, 975), (362, 975), (363, 975), (364, 975), (364, 974), (364, 973), (364, 972), (365, 971), (366, 970), (367, 970), (368, 970), (368, 969), (368, 968), (369, 967), (370, 966), (371, 966), (372, 966), (373, 966), (373, 965), (373, 964), (374, 963), (375, 962), (376, 962), (377, 962), (377, 961), (377, 960), (377, 959), (377, 958), (377, 957), (377, 956), (378, 955), (379, 954), (380, 954), (381, 954), (381, 953), (381, 952), (382, 951), (383, 950), (384, 950), (385, 950), (385, 949), (385, 948), (385, 947), (386, 946), (387, 945), (388, 945), (389, 945), (389, 944), (389, 943), (389, 942), (389, 941), (389, 940), (389, 939), (389, 938), (389, 937), (389, 936), (389, 935), (390, 934), (391, 933), (392, 933), (393, 933), (393, 932), (393, 931), (393, 930), (393, 929), (393, 928), (393, 927), (394, 926), (395, 925), (396, 925), (397, 925), (398, 925), (399, 925), (400, 925), (401, 925), (402, 925), (402, 924), (402, 923), (402, 922), (403, 921), (404, 920), (405, 920), (406, 920), (407, 920), (408, 920), (409, 920), (410, 920), (410, 919), (410, 918), (411, 917), (412, 916), (413, 916), (414, 916), (414, 915), (414, 914), (415, 913), (416, 912), (417, 912), (418, 912), (418, 911), (418, 910), (419, 909), (420, 908), (421, 908), (422, 908), (423, 908), (423, 907), (423, 906), (424, 905), (425, 904), (426, 904), (427, 904), (427, 903), (427, 902), (427, 901), (427, 900), (427, 899), (427, 898), (427, 897), (428, 896), (429, 895), (430, 895), (431, 895), (431, 894), (431, 893), (432, 892), (433, 891), (434, 891), (435, 891), (435, 890), (435, 889), (435, 888), (435, 887), (435, 886), (435, 885), (435, 884), (435, 883), (435, 882), (435, 881), (436, 880), (437, 879), (438, 879), (439, 879), (440, 879), (441, 879), (442, 879), (443, 879), (443, 878), (443, 877), (444, 876), (445, 875), (446, 875), (447, 875), (448, 875), (449, 875), (450, 875), (451, 875), (452, 875), (452, 874), (452, 873), (452, 872), (452, 871), (452, 870), (452, 869), (452, 868), (453, 867), (454, 866), (455, 866), (456, 866), (456, 865), (456, 864), (457, 863), (458, 862), (459, 862), (460, 862), (460, 861), (460, 860), (460, 859), (460, 858), (460, 857), (460, 856), (460, 855), (460, 854), (460, 853), (460, 852), (461, 851), (462, 850), (463, 850), (464, 850), (464, 849), (464, 848), (464, 847), (465, 846), (466, 845), (467, 845), (468, 845), (468, 844), (468, 843), (469, 842), (470, 841), (471, 841), (472, 841), (473, 841), (474, 841), (475, 841), (476, 841), (477, 841), (477, 840), (477, 839), (478, 838), (479, 837), (480, 837), (481, 837), (482, 837), (483, 837), (484, 837), (485, 837), (485, 836), (485, 835), (486, 834), (487, 833), (488, 833), (489, 833), (489, 832), (489, 831), (490, 830), (491, 829), (492, 829), (493, 829), (493, 828), (493, 827), (494, 826), (495, 825), (496, 825), (497, 825), (498, 825), (498, 824), (498, 823), (498, 822), (498, 821), (498, 820), (498, 819), (498, 818), (498, 817), (498, 816), (498, 815), (498, 814), (498, 813), (498, 812), (498, 811), (498, 810), (499, 809), (500, 808), (501, 808), (502, 808), (502, 807), (502, 806), (503, 805), (504, 804), (505, 804), (506, 804), (506, 803), (506, 802), (507, 801), (508, 800), (509, 800), (510, 800), (510, 799), (510, 798), (510, 797), (511, 796), (512, 795), (513, 795), (514, 795), (514, 794), (514, 793), (515, 792), (516, 791), (517, 791), (518, 791), (518, 790), (518, 789), (518, 788), (518, 787), (518, 786), (518, 785), (519, 784), (520, 783), (521, 783), (522, 783), (523, 783), (523, 782), (523, 781), (524, 780), (525, 779), (526, 779), (527, 779), (527, 778), (527, 777), (528, 776), (529, 775), (530, 775), (531, 775), (531, 774), (531, 773), (531, 772), (531, 771), (531, 770), (531, 769), (531, 768), (532, 767), (533, 766), (534, 766), (535, 766), (536, 766), (537, 766), (538, 766), (539, 766), (540, 766), (541, 766), (542, 766), (543, 766), (543, 765), (543, 764), (544, 763), (545, 762), (546, 762), (547, 762), (548, 762), (548, 761), (548, 760), (549, 759), (550, 758), (551, 758), (552, 758), (552, 757), (552, 756), (552, 755), (552, 754), (552, 753), (552, 752), (553, 751), (554, 750), (555, 750), (556, 750), (556, 749), (556, 748), (556, 747), (556, 746), (556, 745), (556, 744), (556, 743), (557, 742), (558, 741), (559, 741), (560, 741), (560, 740), (560, 739), (561, 738), (562, 737), (563, 737), (564, 737), (564, 736), (564, 735), (564, 734), (564, 733), (564, 732), (564, 731), (564, 730), (564, 729), (564, 728), (564, 727), (565, 726), (566, 725), (567, 725), (568, 725), (568, 724), (568, 723), (568, 722), (568, 721), (568, 720), (568, 719), (568, 718), (568, 717), (568, 716), (568, 715), (568, 714), (568, 713), (568, 712), (568, 711), (568, 710), (569, 709), (570, 708), (571, 708), (572, 708), (573, 708), (573, 707), (573, 706), (573, 705), (573, 704), (573, 703), (573, 702), (574, 701), (575, 700), (576, 700), (577, 700), (577, 699), (577, 698), (577, 697), (577, 696), (577, 695), (577, 694), (577, 693), (577, 692), (577, 691), (577, 690), (577, 689), (577, 688), (577, 687), (577, 686), (577, 685), (577, 684), (577, 683), (577, 682), (577, 681), (578, 680), (579, 679), (580, 679), (581, 679), (581, 678), (581, 677), (582, 676), (583, 675), (584, 675), (585, 675), (585, 674), (585, 673), (585, 672), (585, 671), (585, 670), (585, 669), (585, 668), (585, 667), (585, 666), (585, 665), (585, 664), (585, 663), (585, 662), (585, 661), (585, 660), (586, 659), (587, 658), (588, 658), (589, 658), (589, 657), (589, 656), (589, 655), (589, 654), (589, 653), (589, 652), (589, 651), (589, 650), (589, 649), (589, 648), (589, 647), (590, 646), (591, 645), (592, 645), (593, 645), (593, 644), (593, 643), (593, 642), (593, 641), (593, 640), (593, 639), (594, 638), (595, 637), (596, 637), (597, 637), (598, 637), (598, 636), (598, 635), (599, 634), (600, 633), (601, 633), (602, 633), (602, 632), (602, 631), (602, 630), (602, 629), (602, 628), (602, 627), (603, 626), (604, 625), (605, 625), (606, 625), (606, 624), (606, 623), (606, 622), (606, 621), (606, 620), (606, 619), (606, 618), (606, 617), (606, 616), (606, 615), (606, 614), (606, 613), (606, 612), (606, 611), (606, 610), (607, 609), (608, 608), (609, 608), (610, 608), (610, 607), (610, 606), (610, 605), (610, 604), (610, 603), (610, 602), (610, 601), (610, 600), (610, 599), (610, 598), (610, 597), (611, 596), (612, 595), (613, 595), (614, 595), (614, 594), (614, 593), (614, 592), (614, 591), (614, 590), (614, 589), (614, 588), (614, 587), (614, 586), (614, 585), (614, 584), (614, 583), (614, 582), (614, 581), (615, 580), (616, 579), (617, 579), (618, 579), (618, 578), (618, 577), (618, 576), (618, 575), (618, 574), (618, 573), (618, 572), (618, 571), (618, 570), (618, 569), (618, 568), (619, 567), (620, 566), (621, 566), (622, 566), (623, 566), (623, 565), (623, 564), (623, 563), (623, 562), (623, 561), (623, 560)]
#
# xpoints = [p[0] for p in points]
# ypoints = [p[1] for p in points]
#
# xvals, yvals = bezier_curve(points, nTimes=1000)
# plt.plot(xvals, yvals)
# plt.plot(xpoints, ypoints, "ro")
# # for nr in range(len(points)):
# # plt.text(points[nr][0], points[nr][1], nr)
#
# plt.show()
def point_inside_polygon(x, y, poly, include_edges=True):
'''
Test if point (x,y) is inside polygon poly.
poly is N-vertices polygon defined as
[(x1,y1),...,(xN,yN)] or [(x1,y1),...,(xN,yN),(x1,y1)]
(function works fine in both cases)
Geometrical idea: point is inside polygon if horisontal beam
to the right from point crosses polygon even number of times.
Works fine for non-convex polygons.
'''
n = len(poly)
inside = False
p1x, p1y = poly[0]
for i in range(1, n + 1):
p2x, p2y = poly[i % n]
if p1y == p2y:
if y == p1y:
if min(p1x, p2x) <= x <= max(p1x, p2x):
# point is on horisontal edge
inside = include_edges
break
elif x < min(p1x, p2x): # point is to the left from current edge
inside = not inside
else: # p1y!= p2y
if min(p1y, p2y) <= y <= max(p1y, p2y):
xinters = (y - p1y) * (p2x - p1x) / float(p2y - p1y) + p1x
if x == xinters: # point is right on the edge
inside = include_edges
break
if x < xinters: # point is to the left from current edge
inside = not inside
p1x, p1y = p2x, p2y
return inside
polygon = [(0,0),(1,0),(1,2),(0,2),(0,0)]
is_inside = point_inside_polygon(1, 1, polygon)
print(is_inside)
is_inside = point_inside_polygon(1, 1, polygon, include_edges=False)
print(is_inside)