Cell[CellGroupData[{Cell[BoxData[ RowBox[{ RowBox[{"ReturnLines", "[", "ptlist_", "]"}], " ", ":=", RowBox[{"First", "/@", RowBox[{"Select", "[", RowBox[{ RowBox[{"With", "[", RowBox[{ RowBox[{"{", RowBox[{"linelist", " ", "=", RowBox[{"Split", "[", RowBox[{ RowBox[{"Sort", "[", RowBox[{ RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"First", "[", RowBox[{"y", "/.", RowBox[{"Solve", "[", RowBox[{ RowBox[{ RowBox[{"Det", "[", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"x", ",", "y", ",", "1"}], "}"}], ",", RowBox[{"Append", "[", RowBox[{ RowBox[{"#", "[", RowBox[{"[", "1", "]"}], "]"}], ",", "1"}], "]"}], ",", RowBox[{"Append", "[", RowBox[{ RowBox[{"#", "[", RowBox[{"[", "2", "]"}], "]"}], ",", "1"}], "]"}]}], "}"}], "]"}], "==", "0"}], ",", "y"}], "]"}]}], "]"}], ",", RowBox[{"{", RowBox[{ RowBox[{"#", "[", RowBox[{"[", "1", "]"}], "]"}], ",", RowBox[{"#", "[", RowBox[{"[", "2", "]"}], "]"}]}], "}"}]}], "}"}], "&"}], "/@", RowBox[{"Flatten", "[", RowBox[{ RowBox[{"Table", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"ptlist", "[", RowBox[{"[", "a", "]"}], "]"}], ",", RowBox[{"ptlist", "[", RowBox[{"[", "b", "]"}], "]"}]}], "}"}], ",", RowBox[{"{", RowBox[{"a", ",", "1", ",", RowBox[{ RowBox[{"Length", "[", "ptlist", "]"}], "-", "1"}]}], "}"}], ",", RowBox[{"{", RowBox[{"b", ",", RowBox[{"a", "+", "1"}], ",", RowBox[{"Length", "[", "ptlist", "]"}]}], "}"}]}], "]"}], ",", "1"}], "]"}]}], "]"}], ",", RowBox[{ RowBox[{ RowBox[{"First", "[", "#1", "]"}], "==", RowBox[{"First", "[", "#2", "]"}]}], "&"}]}], "]"}]}], "}"}], ",", RowBox[{"Join", "[", RowBox[{ RowBox[{"Map", "[", RowBox[{ RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"First", "[", "#", "]"}], ",", RowBox[{"{", RowBox[{"f", ",", RowBox[{"Last", "[", "#", "]"}]}], "}"}]}], "}"}], "&"}], ",", RowBox[{"Table", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"Union", "[", RowBox[{"Flatten", "[", RowBox[{ RowBox[{"Last", "/@", RowBox[{ RowBox[{"Drop", "[", RowBox[{"linelist", ",", RowBox[{"-", "1"}]}], "]"}], "[", RowBox[{"[", "n", "]"}], "]"}]}], ",", "1"}], "]"}], "]"}], ",", RowBox[{ RowBox[{"Drop", "[", RowBox[{"linelist", ",", RowBox[{"-", "1"}]}], "]"}], "[", RowBox[{"[", RowBox[{"n", ",", "1", ",", "1"}], "]"}], "]"}]}], "}"}], ",", RowBox[{"{", RowBox[{"n", ",", "1", ",", RowBox[{"Length", "[", RowBox[{"Drop", "[", RowBox[{"linelist", ",", RowBox[{"-", "1"}]}], "]"}], "]"}]}], "}"}]}], "]"}]}], "]"}], ",", " ", RowBox[{"Map", "[", RowBox[{ RowBox[{ RowBox[{"{", RowBox[{"#", ",", RowBox[{"{", RowBox[{"x", ",", RowBox[{"#", "[", RowBox[{"[", RowBox[{"1", ",", "1"}], "]"}], "]"}]}], "}"}]}], "}"}], "&"}], ",", RowBox[{"Split", "[", RowBox[{ RowBox[{"Union", "[", RowBox[{"Flatten", "[", RowBox[{ RowBox[{"Last", "/@", RowBox[{"Last", "[", "linelist", "]"}]}], ",", "1"}], "]"}], "]"}], ",", RowBox[{ RowBox[{ RowBox[{"First", "[", "#1", "]"}], "==", RowBox[{"First", "[", "#2", "]"}]}], "&"}]}], "]"}]}], "]"}]}], "]"}]}], "]"}], ",", RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"Length", "[", RowBox[{"#", "[", RowBox[{"[", "1", "]"}], "]"}], "]"}], ">", "2"}], ")"}], "&"}]}], "]"}]}]}]], "Input", CellGroupingRules->{"GroupTogetherGrouping", 10000.}, CellChangeTimes->{3.760772286263693*^9}, CellID->60572844], Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{"ClosedLine", "[", "x_", "]"}], ":=", RowBox[{"Line", "[", RowBox[{"Append", "[", RowBox[{"x", ",", RowBox[{"First", "[", "x", "]"}]}], "]"}], "]"}]}], ";"}]], "Input", CellGroupingRules->{"GroupTogetherGrouping", 10000.}, CellChangeTimes->{3.7607722862637978`*^9}, CellID->507984414], Cell[BoxData[ RowBox[{ RowBox[{"SegmentIntersectionQ", "[", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"x1_", ",", "y1_"}], "}"}], ",", RowBox[{"{", RowBox[{"x2_", ",", "y2_"}], "}"}]}], "}"}], ",", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"x3_", ",", "y3_"}], "}"}], ",", RowBox[{"{", RowBox[{"x4_", ",", "y4_"}], "}"}]}], "}"}]}], "}"}], "]"}], " ", ":=", " ", " ", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{ RowBox[{"Length", "[", RowBox[{"Intersection", "[", RowBox[{ RowBox[{"SegmentPoints", "[", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"x1", ",", "y1"}], "}"}], ",", RowBox[{"{", RowBox[{"x2", ",", "y2"}], "}"}]}], "}"}], "]"}], ",", RowBox[{"SegmentPoints", "[", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"x3", ",", "y3"}], "}"}], ",", RowBox[{"{", RowBox[{"x4", ",", "y4"}], "}"}]}], "}"}], "]"}]}], "]"}], "]"}], ">", "1"}], "||", RowBox[{ RowBox[{"Length", "[", RowBox[{"Intersection", "[", RowBox[{ RowBox[{"SegmentPoints", "[", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"x1", ",", "y1"}], "}"}], ",", RowBox[{"{", RowBox[{"x3", ",", "y3"}], "}"}]}], "}"}], "]"}], ",", RowBox[{"SegmentPoints", "[", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"x2", ",", "y2"}], "}"}], ",", RowBox[{"{", RowBox[{"x4", ",", "y4"}], "}"}]}], "}"}], "]"}]}], "]"}], "]"}], ">", "1"}]}], ",", "True", ",", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{ RowBox[{"{", RowBox[{"x1", ",", "y1"}], "}"}], "==", RowBox[{"{", RowBox[{"x2", ",", "y2"}], "}"}]}], "||", RowBox[{ RowBox[{"{", RowBox[{"x3", ",", "y3"}], "}"}], "==", RowBox[{"{", RowBox[{"x4", ",", "y4"}], "}"}]}]}], ",", "True", ",", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{ RowBox[{"{", RowBox[{"x1", ",", "y1"}], "}"}], "==", RowBox[{"{", RowBox[{"x3", ",", "y3"}], "}"}]}], "||", RowBox[{ RowBox[{"{", RowBox[{"x1", ",", "y1"}], "}"}], "==", RowBox[{"{", RowBox[{"x4", ",", "y4"}], "}"}]}], "||", RowBox[{ RowBox[{"{", RowBox[{"x2", ",", "y2"}], "}"}], "==", RowBox[{"{", RowBox[{"x3", ",", "y3"}], "}"}]}], "||", RowBox[{ RowBox[{"{", RowBox[{"x2", ",", "y2"}], "}"}], "==", RowBox[{"{", RowBox[{"x4", ",", "y4"}], "}"}]}]}], ",", "False", ",", RowBox[{"With", "[", RowBox[{ RowBox[{"{", RowBox[{"LineIntersection", " ", "=", " ", RowBox[{"Solve", "[", RowBox[{ RowBox[{"{", " ", RowBox[{ RowBox[{ RowBox[{"Det", "[", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"x", ",", "y", ",", "1"}], "}"}], ",", RowBox[{"{", RowBox[{"x1", ",", "y1", ",", "1"}], "}"}], ",", RowBox[{"{", RowBox[{"x2", ",", "y2", ",", "1"}], "}"}]}], "}"}], "]"}], "==", "0"}], ",", " ", RowBox[{ RowBox[{"Det", "[", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"x", ",", "y", ",", "1"}], "}"}], ",", RowBox[{"{", RowBox[{"x3", ",", "y3", ",", "1"}], "}"}], ",", RowBox[{"{", RowBox[{"x4", ",", "y4", ",", "1"}], "}"}]}], "}"}], "]"}], "==", "0"}]}], "}"}], ",", " ", RowBox[{"{", RowBox[{"x", ",", "y"}], "}"}]}], "]"}]}], "}"}], ",", " ", RowBox[{"With", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"LineIntX", " ", "=", " ", RowBox[{"FullSimplify", "[", RowBox[{ RowBox[{"Flatten", "[", RowBox[{ RowBox[{"{", RowBox[{"x", ",", "y"}], "}"}], "/.", "LineIntersection"}], "]"}], "[", RowBox[{"[", "1", "]"}], "]"}], "]"}]}], ",", " ", RowBox[{"LineIntY", " ", "=", " ", RowBox[{"FullSimplify", "[", RowBox[{ RowBox[{"Flatten", "[", RowBox[{ RowBox[{"{", RowBox[{"x", ",", "y"}], "}"}], "/.", "LineIntersection"}], "]"}], "[", RowBox[{"[", "2", "]"}], "]"}], "]"}]}]}], "}"}], ",", " ", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"LineIntersection", " ", "!=", " ", RowBox[{"{", "}"}]}], " ", "&&", " ", RowBox[{"(", RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"Positive", "[", RowBox[{"FullSimplify", "[", RowBox[{"x1", " ", "-", " ", "LineIntX"}], "]"}], "]"}], " ", "&&", " ", RowBox[{"Positive", "[", RowBox[{"FullSimplify", "[", RowBox[{"LineIntX", " ", "-", " ", "x2"}], "]"}], "]"}]}], ")"}], " ", "||", " ", RowBox[{"(", RowBox[{ RowBox[{"Negative", "[", RowBox[{"FullSimplify", "[", RowBox[{"x1", " ", "-", " ", "LineIntX"}], "]"}], "]"}], " ", "&&", " ", RowBox[{"Negative", "[", RowBox[{"FullSimplify", "[", RowBox[{"LineIntX", " ", "-", " ", "x2"}], "]"}], "]"}]}], ")"}]}], ")"}], "||", " ", RowBox[{"(", RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"Positive", "[", RowBox[{"FullSimplify", "[", RowBox[{"y1", " ", "-", " ", "LineIntY"}], "]"}], "]"}], " ", "&&", " ", RowBox[{"Positive", "[", RowBox[{"FullSimplify", "[", RowBox[{"LineIntY", " ", "-", " ", "y2"}], "]"}], "]"}]}], ")"}], " ", "||", " ", RowBox[{"(", RowBox[{ RowBox[{"Negative", "[", RowBox[{"FullSimplify", "[", RowBox[{"y1", " ", "-", " ", "LineIntY"}], "]"}], "]"}], " ", "&&", " ", RowBox[{"Negative", "[", RowBox[{"FullSimplify", "[", RowBox[{"LineIntY", " ", "-", " ", "y2"}], "]"}], "]"}]}], ")"}]}], ")"}]}], ")"}], " ", "&&", " ", RowBox[{"(", RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"Positive", "[", RowBox[{"FullSimplify", "[", RowBox[{"x3", "-", "LineIntX"}], "]"}], "]"}], "&&", RowBox[{"Positive", "[", RowBox[{"FullSimplify", "[", RowBox[{"LineIntX", "-", "x4"}], "]"}], "]"}]}], ")"}], "||", RowBox[{"(", RowBox[{ RowBox[{"Negative", "[", RowBox[{"FullSimplify", "[", RowBox[{"x3", "-", "LineIntX"}], "]"}], "]"}], "&&", RowBox[{"Negative", "[", RowBox[{"FullSimplify", "[", RowBox[{"LineIntX", "-", "x4"}], "]"}], "]"}]}], ")"}]}], ")"}], "||", RowBox[{"(", RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"Positive", "[", RowBox[{"FullSimplify", "[", RowBox[{"y3", "-", "LineIntY"}], "]"}], "]"}], "&&", RowBox[{"Positive", "[", RowBox[{"FullSimplify", "[", RowBox[{"LineIntY", "-", "y4"}], "]"}], "]"}]}], ")"}], "||", RowBox[{"(", RowBox[{ RowBox[{"Negative", "[", RowBox[{"FullSimplify", "[", RowBox[{"y3", "-", "LineIntY"}], "]"}], "]"}], "&&", RowBox[{"Negative", "[", RowBox[{"FullSimplify", "[", RowBox[{"LineIntY", "-", "y4"}], "]"}], "]"}]}], ")"}]}], ")"}]}], ")"}]}], ")"}]}], " ", ",", "True", ",", "False"}], "]"}]}], "]"}]}], "]"}]}], "]"}]}], "]"}]}], "]"}]}]], "Input", CellGroupingRules->{"GroupTogetherGrouping", 10000.}, CellChangeTimes->{ 3.35696210375764*^9, {3.3719154639382005`*^9, 3.3719155084344015`*^9}, {3.3719160819125767`*^9, 3.371916085948501*^9}, {3.371928493249601*^9, 3.371928493750341*^9}, {3.371928545426709*^9, 3.371928632926017*^9}, {3.3719287342858076`*^9, 3.371928786322708*^9}, {3.3719290063025475`*^9, 3.37192911863976*^9}, {3.3719291728352537`*^9, 3.3719291752891245`*^9}, {3.3719877858110433`*^9, 3.3719878121201854`*^9}, 3.760772286263936*^9}, CellID->45645772], Cell[BoxData[ RowBox[{ RowBox[{"SegmentPoints", "[", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"x1_", ",", "y1_"}], "}"}], ",", RowBox[{"{", RowBox[{"x2_", ",", "y2_"}], "}"}]}], "}"}], "]"}], " ", ":=", " ", RowBox[{"If", "[", RowBox[{ RowBox[{"x1", "==", "x2"}], ",", RowBox[{"Table", "[", RowBox[{ RowBox[{"{", RowBox[{"x1", ",", "y"}], "}"}], ",", RowBox[{"{", RowBox[{"y", ",", RowBox[{"Min", "[", RowBox[{"y1", ",", "y2"}], "]"}], ",", RowBox[{"Max", "[", RowBox[{"y1", ",", "y2"}], "]"}]}], "}"}]}], "]"}], ",", RowBox[{"Select", "[", RowBox[{ RowBox[{"Table", "[", RowBox[{ RowBox[{"{", RowBox[{"x", ",", RowBox[{ RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"(", RowBox[{"y2", "-", "y1"}], ")"}], "/", RowBox[{"(", RowBox[{"x2", "-", "x1"}], ")"}]}], ")"}], RowBox[{"(", RowBox[{"x", "-", "x1"}], ")"}]}], "+", "y1"}]}], "}"}], ",", RowBox[{"{", RowBox[{"x", ",", RowBox[{"Min", "[", RowBox[{"x1", ",", "x2"}], "]"}], ",", RowBox[{"Max", "[", RowBox[{"x1", ",", "x2"}], "]"}]}], "}"}]}], "]"}], ",", RowBox[{ RowBox[{"IntegerQ", "[", RowBox[{"Last", "[", "#", "]"}], "]"}], "&"}]}], "]"}]}], "]"}]}]], "Input", CellGroupingRules->{"GroupTogetherGrouping", 10000.}, CellChangeTimes->{ 3.35696210375764*^9, {3.3719199138606205`*^9, 3.3719200914825907`*^9}, {3.3719201329183693`*^9, 3.371920163767341*^9}, {3.3719202862862196`*^9, 3.371920320136115*^9}, {3.3719203542663417`*^9, 3.3719204171504574`*^9}, {3.3719214391951504`*^9, 3.3719215252319727`*^9}, {3.3719215847110577`*^9, 3.3719216856322126`*^9}, 3.3719218372893577`*^9, { 3.3719218878350625`*^9, 3.3719219130227876`*^9}, { 3.3719219573491774`*^9, 3.371921970538933*^9}, { 3.3719245531820292`*^9, 3.371924554443894*^9}, 3.760772286264043*^9}, CellID->934293997], Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{"BorderPoints", "[", "a_List", "]"}], " ", ":=", " ", RowBox[{"Union", "[", RowBox[{"Flatten", "[", RowBox[{ RowBox[{"Map", "[", RowBox[{"SegmentPoints", ",", RowBox[{"Partition", "[", RowBox[{"a", ",", "2", ",", "1", ",", "1"}], "]"}]}], "]"}], ",", "1"}], "]"}], "]"}]}], ";"}]], "Input", CellGroupingRules->{"GroupTogetherGrouping", 10000.}, CellChangeTimes->{ 3.35696210375764*^9, {3.3719252796856656`*^9, 3.371925303711171*^9}, {3.371927585883795*^9, 3.3719275869153194`*^9}, 3.760772286264134*^9}, CellID->446962675], Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{"BorderPointsCheck", "[", "a_List", "]"}], " ", ":=", " ", RowBox[{ RowBox[{"Length", "[", RowBox[{"Flatten", "[", RowBox[{ RowBox[{"Map", "[", RowBox[{"SegmentPoints", ",", RowBox[{"Partition", "[", RowBox[{"a", ",", "2", ",", "1", ",", "1"}], "]"}]}], "]"}], ",", "1"}], "]"}], "]"}], "-", RowBox[{"Length", "[", "a", "]"}], "-", RowBox[{"Length", "[", RowBox[{"BorderPoints", "[", "a", "]"}], "]"}]}]}], ";"}]], "Input", CellGroupingRules->{"GroupTogetherGrouping", 10000.}, CellChangeTimes->{ 3.35696210375764*^9, {3.3719252796856656`*^9, 3.371925303711171*^9}, {3.371927585883795*^9, 3.3719275869153194`*^9}, {3.371988201768195*^9, 3.371988251812684*^9}, 3.7607722862642307`*^9}, CellID->357688214], Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{"SimplePolygonQ", "[", "a_List", "]"}], " ", ":=", " ", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{ RowBox[{"Union", "[", RowBox[{"Flatten", "[", RowBox[{"With", "[", RowBox[{ RowBox[{"{", RowBox[{"data", "=", RowBox[{"Partition", "[", RowBox[{"a", ",", "2", ",", "1", ",", "1"}], "]"}]}], "}"}], ",", RowBox[{"Table", "[", RowBox[{ RowBox[{"SegmentIntersectionQ", "[", RowBox[{"{", RowBox[{ RowBox[{"data", "[", RowBox[{"[", "m", "]"}], "]"}], ",", RowBox[{"data", "[", RowBox[{"[", "n", "]"}], "]"}]}], "}"}], "]"}], ",", RowBox[{"{", RowBox[{"m", ",", "1", ",", RowBox[{ RowBox[{"Length", "[", "data", "]"}], "-", "1"}]}], "}"}], ",", RowBox[{"{", RowBox[{"n", ",", RowBox[{"m", "+", "1"}], ",", RowBox[{"Length", "[", "data", "]"}]}], "}"}]}], "]"}]}], "]"}], "]"}], "]"}], "==", RowBox[{"{", "False", "}"}]}], "&&", RowBox[{ RowBox[{"BorderPointsCheck", "[", "a", "]"}], "==", "0"}]}], ",", "True", ",", "False"}], "]"}]}], ";"}]], "Input", CellGroupingRules->{"GroupTogetherGrouping", 10000.}, CellChangeTimes->{ 3.35696210375764*^9, {3.3719158745982723`*^9, 3.371915962497294*^9}, {3.371916020191981*^9, 3.3719160294555783`*^9}, {3.3719884837494564`*^9, 3.371988496074475*^9}, 3.760772286264331*^9}, CellID->53738867], Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{"InteriorPoints", "[", "a_List", "]"}], " ", ":=", " ", RowBox[{"Last", "/@", RowBox[{"With", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"data", "=", RowBox[{"Partition", "[", RowBox[{"a", ",", "2", ",", "1", ",", "1"}], "]"}]}], ",", RowBox[{"grid", "=", RowBox[{"Complement", "[", RowBox[{ RowBox[{"Flatten", "[", RowBox[{ RowBox[{"Table", "[", RowBox[{ RowBox[{"{", RowBox[{"b", ",", "c"}], "}"}], ",", RowBox[{"{", RowBox[{"b", ",", "1", ",", "7"}], "}"}], ",", RowBox[{"{", RowBox[{"c", ",", "1", ",", "7"}], "}"}]}], "]"}], ",", "1"}], "]"}], ",", RowBox[{"BorderPoints", "[", "a", "]"}]}], "]"}]}]}], "}"}], ",", RowBox[{"Select", "[", RowBox[{ RowBox[{"Table", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"OddQ", "[", RowBox[{"Count", "[", RowBox[{ RowBox[{"Table", "[", RowBox[{ RowBox[{"SegmentIntersectionQ", "[", RowBox[{"{", RowBox[{ RowBox[{"data", "[", RowBox[{"[", "m", "]"}], "]"}], ",", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"N", "[", RowBox[{"1", "/", "E"}], "]"}], ",", RowBox[{"N", "[", RowBox[{"1", "/", "Pi"}], "]"}]}], "}"}], ",", RowBox[{"grid", "[", RowBox[{"[", "n", "]"}], "]"}]}], "}"}]}], "}"}], "]"}], ",", RowBox[{"{", RowBox[{"m", ",", "1", ",", RowBox[{"Length", "[", "data", "]"}]}], "}"}]}], "]"}], ",", "True"}], "]"}], "]"}], ",", RowBox[{"grid", "[", RowBox[{"[", "n", "]"}], "]"}]}], "}"}], ",", RowBox[{"{", RowBox[{"n", ",", "1", ",", RowBox[{"Length", "[", "grid", "]"}]}], "}"}]}], "]"}], ",", RowBox[{ RowBox[{ RowBox[{"First", "[", "#", "]"}], "==", "True"}], "&"}]}], "]"}]}], "]"}]}]}], ";"}]], "Input", CellGroupingRules->{"GroupTogetherGrouping", 10000.}, CellChangeTimes->{ 3.35696210375764*^9, {3.3719224275210075`*^9, 3.3719224645398083`*^9}, 3.3719224961014147`*^9, { 3.371922675345604*^9, 3.371922719069348*^9}, {3.371922760870288*^9, 3.3719227989157534`*^9}, {3.3719228303415685`*^9, 3.37192288888692*^9}, {3.37192294146357*^9, 3.371922967231136*^9}, {3.3719230165330114`*^9, 3.3719230254259768`*^9}, 3.371924115645432*^9, { 3.3719241635562353`*^9, 3.371924200510847*^9}, { 3.371924235412425*^9, 3.3719242424127703`*^9}, { 3.371924285947106*^9, 3.371924395839506*^9}, { 3.3719244265448833`*^9, 3.371924497960422*^9}, { 3.3719254317103295`*^9, 3.371925449516644*^9}, { 3.371927943811835*^9, 3.3719279454542947`*^9}, 3.7607722862644176`*^9}, CellID->679535597], Cell[BoxData[ RowBox[{"Manipulate", "[", RowBox[{ RowBox[{ RowBox[{"SeedRandom", "[", "sr", "]"}], ";", RowBox[{"DynamicModule", "[", RowBox[{ RowBox[{"{", RowBox[{"data", "=", RowBox[{"RandomInteger", "[", RowBox[{ RowBox[{"{", RowBox[{"1", ",", "7"}], "}"}], ",", RowBox[{"{", RowBox[{"10", ",", "2"}], "}"}]}], "]"}]}], "}"}], ",", RowBox[{"LocatorPane", "[", RowBox[{ RowBox[{"Dynamic", "[", RowBox[{"data", "[", RowBox[{"[", RowBox[{"Range", "[", "n", "]"}], "]"}], "]"}], "]"}], ",", RowBox[{"Dynamic", "[", RowBox[{"Block", "[", RowBox[{ RowBox[{"{", RowBox[{"fit", ",", "x"}], "}"}], ",", " ", RowBox[{"With", "[", RowBox[{ RowBox[{"{", RowBox[{"poly", "=", RowBox[{"Map", "[", RowBox[{ RowBox[{ RowBox[{"Min", "[", RowBox[{"7", ",", RowBox[{"Max", "[", RowBox[{"{", RowBox[{"1", ",", RowBox[{"Round", "[", "#", "]"}]}], "}"}], "]"}]}], "]"}], "&"}], ",", RowBox[{"Take", "[", RowBox[{"data", ",", "n"}], "]"}], ",", RowBox[{"{", "2", "}"}]}], "]"}]}], "}"}], ",", RowBox[{"With", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"border", "=", RowBox[{"BorderPoints", "[", "poly", "]"}]}], ",", RowBox[{"interior", "=", RowBox[{"InteriorPoints", "[", "poly", "]"}]}]}], "}"}], ",", RowBox[{"Graphics", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"RGBColor", "[", RowBox[{".7", ",", ".7", ",", ".4", ",", ".3"}], "]"}], ",", RowBox[{"Polygon", "[", "poly", "]"}]}], "}"}], ",", RowBox[{"ClosedLine", "[", "poly", "]"}], ",", RowBox[{"AbsolutePointSize", "[", "4", "]"}], ",", "Red", ",", RowBox[{"Point", "/@", RowBox[{"Complement", "[", RowBox[{ RowBox[{"Flatten", "[", RowBox[{ RowBox[{"Table", "[", RowBox[{ RowBox[{"{", RowBox[{"a", ",", "b"}], "}"}], ",", RowBox[{"{", RowBox[{"a", ",", "1", ",", "7"}], "}"}], ",", RowBox[{"{", RowBox[{"b", ",", "1", ",", "7"}], "}"}]}], "]"}], ",", "1"}], "]"}], ",", RowBox[{"Union", "[", RowBox[{"border", ",", "interior"}], "]"}]}], "]"}]}], ",", "Green", ",", RowBox[{"Point", "/@", "interior"}], ",", RowBox[{"AbsolutePointSize", "[", "7", "]"}], ",", "Blue", ",", RowBox[{"Point", "/@", "border"}], ",", RowBox[{"AbsolutePointSize", "[", "12", "]"}], ",", "Blue", ",", RowBox[{"Point", "/@", "poly"}]}], " ", "}"}], ",", " ", RowBox[{"PlotRange", "->", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{".5", ",", "7.5"}], "}"}], ",", RowBox[{"{", RowBox[{".5", ",", "7.5"}], "}"}]}], "}"}]}], ",", RowBox[{"PlotLabel", "->", " ", RowBox[{"Style", "[", RowBox[{ RowBox[{"\"\\"", "<>", RowBox[{"ToString", "[", RowBox[{"Length", "[", "border", "]"}], "]"}], "<>", "\"\<\\nI = interior points = \>\"", "<>", RowBox[{"ToString", "[", RowBox[{"Length", "[", "interior", "]"}], "]"}], "<>", "\"\<\\n\>\"", "<>", RowBox[{"If", "[", RowBox[{ RowBox[{"SimplePolygonQ", "[", "poly", "]"}], ",", RowBox[{"\"\\"", "<>", RowBox[{"ToString", "[", RowBox[{"Length", "[", "border", "]"}], "]"}], "<>", "\"\\"", "<>", RowBox[{"ToString", "[", RowBox[{"Length", "[", "interior", "]"}], "]"}], "<>", "\"\< - 1 = \>\"", "<>", RowBox[{"ToString", "[", RowBox[{"N", "[", RowBox[{ RowBox[{ RowBox[{"Length", "[", "border", "]"}], "/", "2"}], " ", "+", " ", RowBox[{"Length", "[", "interior", "]"}], "-", "1"}], "]"}], "]"}]}], ",", "\"\\""}], "]"}]}], ",", "\"\\"", ",", "12"}], "]"}]}], ",", RowBox[{"ImageSize", "->", " ", RowBox[{"{", RowBox[{"400", ",", "450"}], "}"}]}]}], " ", "]"}]}], "]"}]}], "]"}]}], "]"}], "]"}], ",", " ", RowBox[{"Appearance", "->", "\"\<\>\""}]}], "]"}]}], "]"}]}], ",", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"n", ",", "4", ",", "\"\\""}], "}"}], ",", "3", ",", "10", ",", "1", ",", RowBox[{"Appearance", "->", "\"\\""}]}], "}"}], ",", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"sr", ",", "17", ",", "\"\\""}], "}"}], ",", "1", ",", "100", ",", "1"}], "}"}], ",", RowBox[{"SaveDefinitions", "->", "True"}]}], "]"}]], "Input", CellGroupingRules->{"GroupTogetherGrouping", 10000.}, CellChangeTimes->{ 3.35757176568782*^9, 3.36233416870853*^9, {3.36251277771711*^9, 3.36251280067185*^9}, {3.36251288266184*^9, 3.36251288270872*^9}, 3.36934919953125*^9, {3.3717670988584385`*^9, 3.371767233061413*^9}, {3.371767528506242*^9, 3.371767539962715*^9}, {3.371905112873617*^9, 3.3719051141859493`*^9}, {3.371905147996024*^9, 3.3719051582943225`*^9}, {3.3719052512094173`*^9, 3.3719052703935046`*^9}, {3.3719053497044272`*^9, 3.3719053631483145`*^9}, {3.3719053985111485`*^9, 3.371905408959714*^9}, {3.3719056761205187`*^9, 3.3719056876636343`*^9}, {3.371905729074813*^9, 3.371905742768509*^9}, {3.3719058386922655`*^9, 3.3719058392429643`*^9}, {3.371905916320729*^9, 3.371905951325128*^9}, {3.3719207707311735`*^9, 3.371920773845807*^9}, {3.3719208786825747`*^9, 3.3719209227485747`*^9}, 3.371925679676778*^9, { 3.3719257401561546`*^9, 3.3719257592543783`*^9}, { 3.3719257948369627`*^9, 3.3719258123228035`*^9}, { 3.371925858460987*^9, 3.3719258932724323`*^9}, { 3.371925928915105*^9, 3.3719260703240814`*^9}, { 3.371926496604043*^9, 3.371926568570396*^9}, { 3.3719277186399837`*^9, 3.371927727172593*^9}, { 3.3719277729602585`*^9, 3.37192778041127*^9}, { 3.371927968188345*^9, 3.3719279985738544`*^9}, { 3.3719281384333296`*^9, 3.3719281455640097`*^9}, { 3.3719282143269997`*^9, 3.3719282689080663`*^9}, { 3.3719288274134326`*^9, 3.371928872309781*^9}, { 3.3719292131087856`*^9, 3.3719292455800095`*^9}, { 3.371929481425396*^9, 3.371929489958602*^9}, {3.371929537261809*^9, 3.3719295518644075`*^9}, {3.371929644738139*^9, 3.371929800363073*^9}, {3.3719298358198137`*^9, 3.371929845873067*^9}, {3.3719299131067247`*^9, 3.371929916922211*^9}, {3.371929969818272*^9, 3.3719301000655584`*^9}, 3.3719301309499683`*^9, { 3.371930209223899*^9, 3.3719302606735225`*^9}, { 3.3719304219230723`*^9, 3.3719305284479303`*^9}, { 3.3719307476271324`*^9, 3.371930778775337*^9}, { 3.371930838447686*^9, 3.37193083881826*^9}, { 3.3719313081252975`*^9, 3.371931309596547*^9}, 3.3719314578424487`*^9, {3.371931891030346*^9, 3.371931915280941*^9}, {3.3719322097796574`*^9, 3.371932277281454*^9}, {3.3719323945381155`*^9, 3.371932397703424*^9}, {3.3719871543624706`*^9, 3.3719871575953455`*^9}, {3.3719871951087027`*^9, 3.37198721569701*^9}, 3.371987290974834*^9, {3.371987370483125*^9, 3.3719874192256436`*^9}, 3.3719879005417376`*^9, { 3.3719879566452074`*^9, 3.371987959699752*^9}, { 3.3719882884826107`*^9, 3.3719883297918572`*^9}, { 3.371988509210481*^9, 3.371988517550644*^9}, { 3.3719902977634444`*^9, 3.371990298174014*^9}, { 3.3719904588206244`*^9, 3.371990459721984*^9}, { 3.374877791796875*^9, 3.37487779496875*^9}, { 3.3775338962405367`*^9, 3.3775338973122053`*^9}, { 3.377534201870191*^9, 3.3775342438269672`*^9}, 3.3775342857537313`*^9, {3.3775347310073256`*^9, 3.377534755235967*^9}, {3.3775347873206534`*^9, 3.377534882874173*^9}, {3.3775350837887583`*^9, 3.377535090153655*^9}, {3.3775354543743105`*^9, 3.3775354546347656`*^9}, {3.377536770600932*^9, 3.3775369741437597`*^9}, {3.377537347597674*^9, 3.377537397441334*^9}, {3.377538010743004*^9, 3.3775380318043385`*^9}, {3.379338232365308*^9, 3.37933827517815*^9}, {3.379339204475425*^9, 3.379339227219717*^9}, {3.379446781779558*^9, 3.3794468014380217`*^9}, {3.37945716501875*^9, 3.379457165190625*^9}, {3.380731792234375*^9, 3.380731803765625*^9}, 3.7607722862548656`*^9}, CellID->1496021318], Cell[BoxData[ TagBox[ StyleBox[ DynamicModuleBox[{$CellContext`n$$ = 4, $CellContext`sr$$ = 17, Typeset`show$$ = True, Typeset`bookmarkList$$ = {}, Typeset`bookmarkMode$$ = "Menu", Typeset`animator$$, Typeset`animvar$$ = 1, Typeset`name$$ = "\"untitled\"", Typeset`specs$$ = {{{ Hold[$CellContext`n$$], 4, "number of corners"}, 3, 10, 1}, {{ Hold[$CellContext`sr$$], 17, "random seed"}, 1, 100, 1}}, Typeset`size$$ = {400., {223., 227.}}, Typeset`update$$ = 0, Typeset`initDone$$, Typeset`skipInitDone$$ = False, $CellContext`n$76459$$ = 0, $CellContext`sr$76460$$ = 0}, DynamicBox[Manipulate`ManipulateBoxes[ 1, StandardForm, "Variables" :> {$CellContext`n$$ = 4, $CellContext`sr$$ = 17}, "ControllerVariables" :> { Hold[$CellContext`n$$, $CellContext`n$76459$$, 0], Hold[$CellContext`sr$$, $CellContext`sr$76460$$, 0]}, "OtherVariables" :> { Typeset`show$$, Typeset`bookmarkList$$, Typeset`bookmarkMode$$, Typeset`animator$$, Typeset`animvar$$, Typeset`name$$, Typeset`specs$$, Typeset`size$$, Typeset`update$$, Typeset`initDone$$, Typeset`skipInitDone$$}, "Body" :> (SeedRandom[$CellContext`sr$$]; DynamicModule[{$CellContext`data = RandomInteger[{1, 7}, {10, 2}]}, LocatorPane[ Dynamic[ Part[$CellContext`data, Range[$CellContext`n$$]]], Dynamic[ Block[{$CellContext`fit, $CellContext`x}, With[{$CellContext`poly = Map[Min[7, Max[{1, Round[#]}]]& , Take[$CellContext`data, $CellContext`n$$], {2}]}, With[{$CellContext`border = \ $CellContext`BorderPoints[$CellContext`poly], $CellContext`interior = \ $CellContext`InteriorPoints[$CellContext`poly]}, Graphics[{{ RGBColor[0.7, 0.7, 0.4, 0.3], Polygon[$CellContext`poly]}, $CellContext`ClosedLine[$CellContext`poly], AbsolutePointSize[4], Red, Map[Point, Complement[ Flatten[ Table[{$CellContext`a, $CellContext`b}, \ {$CellContext`a, 1, 7}, {$CellContext`b, 1, 7}], 1], Union[$CellContext`border, $CellContext`interior]]], Green, Map[Point, $CellContext`interior], AbsolutePointSize[7], Blue, Map[Point, $CellContext`border], AbsolutePointSize[12], Blue, Map[Point, $CellContext`poly]}, PlotRange -> {{0.5, 7.5}, {0.5, 7.5}}, PlotLabel -> Style[ StringJoin["B = border points = ", ToString[ Length[$CellContext`border]], "\nI = interior points = ", ToString[ Length[$CellContext`interior]], "\n", If[ $CellContext`SimplePolygonQ[$CellContext`poly], StringJoin["B/2 + I - 1 = ", ToString[ Length[$CellContext`border]], "/2 + ", ToString[ Length[$CellContext`interior]], " - 1 = ", ToString[ N[ Length[$CellContext`border]/2 + Length[$CellContext`interior] - 1]]], "This polygon crosses over itself."]], "Label", 12], ImageSize -> {400, 450}]]]]], Appearance -> ""]]), "Specifications" :> {{{$CellContext`n$$, 4, "number of corners"}, 3, 10, 1, Appearance -> "Labeled"}, {{$CellContext`sr$$, 17, "random seed"}, 1, 100, 1}}, "Options" :> {}, "DefaultOptions" :> {ControllerLinking -> True}], ImageSizeCache->{445., {282., 288.}}, SingleEvaluation->True], Deinitialization:>None, DynamicModuleValues:>{}, Initialization:>({$CellContext`BorderPoints[ Pattern[$CellContext`a, Blank[List]]] := Union[ Flatten[ Map[$CellContext`SegmentPoints, Partition[$CellContext`a, 2, 1, 1]], 1]], $CellContext`SegmentPoints[{{ Pattern[$CellContext`x1, Blank[]], Pattern[$CellContext`y1, Blank[]]}, { Pattern[$CellContext`x2, Blank[]], Pattern[$CellContext`y2, Blank[]]}}] := If[$CellContext`x1 == $CellContext`x2, Table[{$CellContext`x1, $CellContext`y}, {$CellContext`y, Min[$CellContext`y1, $CellContext`y2], Max[$CellContext`y1, $CellContext`y2]}], Select[ Table[{$CellContext`x, (($CellContext`y2 - \ $CellContext`y1)/($CellContext`x2 - $CellContext`x1)) ($CellContext`x - \ $CellContext`x1) + $CellContext`y1}, {$CellContext`x, Min[$CellContext`x1, $CellContext`x2], Max[$CellContext`x1, $CellContext`x2]}], IntegerQ[ Last[#]]& ]], $CellContext`InteriorPoints[ Pattern[$CellContext`a$, Blank[List]]] := Map[Last, With[{$CellContext`data$ = Partition[$CellContext`a$, 2, 1, 1], $CellContext`grid$ = Complement[ Flatten[ Table[{$CellContext`b, $CellContext`c}, {$CellContext`b, 1, 7}, {$CellContext`c, 1, 7}], 1], $CellContext`BorderPoints[$CellContext`a$]]}, Select[ Table[{ OddQ[ Count[ Table[ $CellContext`SegmentIntersectionQ[{ Part[$CellContext`data$, $CellContext`m], {{ N[1/E], N[1/Pi]}, Part[$CellContext`grid$, FE`n$$2390]}}], {$CellContext`m, 1, Length[$CellContext`data$]}], True]], Part[$CellContext`grid$, FE`n$$2390]}, {FE`n$$2390, 1, Length[$CellContext`grid$]}], First[#] == True& ]]], Attributes[$CellContext`a$] = {Temporary}, Attributes[$CellContext`data$] = {Temporary}, Attributes[$CellContext`grid$] = { Temporary}, $CellContext`SegmentIntersectionQ[{{{ Pattern[$CellContext`x1, Blank[]], Pattern[$CellContext`y1, Blank[]]}, { Pattern[$CellContext`x2, Blank[]], Pattern[$CellContext`y2, Blank[]]}}, {{ Pattern[$CellContext`x3, Blank[]], Pattern[$CellContext`y3, Blank[]]}, { Pattern[$CellContext`x4, Blank[]], Pattern[$CellContext`y4, Blank[]]}}}] := If[ Or[Length[ Intersection[ $CellContext`SegmentPoints[{{$CellContext`x1, \ $CellContext`y1}, {$CellContext`x2, $CellContext`y2}}], $CellContext`SegmentPoints[{{$CellContext`x3, \ $CellContext`y3}, {$CellContext`x4, $CellContext`y4}}]]] > 1, Length[ Intersection[ $CellContext`SegmentPoints[{{$CellContext`x1, \ $CellContext`y1}, {$CellContext`x3, $CellContext`y3}}], $CellContext`SegmentPoints[{{$CellContext`x2, \ $CellContext`y2}, {$CellContext`x4, $CellContext`y4}}]]] > 1], True, If[ Or[{$CellContext`x1, $CellContext`y1} == {$CellContext`x2, \ $CellContext`y2}, {$CellContext`x3, $CellContext`y3} == \ {$CellContext`x4, $CellContext`y4}], True, If[ Or[{$CellContext`x1, $CellContext`y1} == {$CellContext`x3, \ $CellContext`y3}, {$CellContext`x1, $CellContext`y1} == \ {$CellContext`x4, $CellContext`y4}, {$CellContext`x2, \ $CellContext`y2} == {$CellContext`x3, $CellContext`y3}, \ {$CellContext`x2, $CellContext`y2} == {$CellContext`x4, \ $CellContext`y4}], False, With[{$CellContext`LineIntersection = Solve[{Det[{{$CellContext`x, $CellContext`y, 1}, {$CellContext`x1, $CellContext`y1, 1}, {$CellContext`x2, $CellContext`y2, 1}}] == 0, Det[{{$CellContext`x, $CellContext`y, 1}, {$CellContext`x3, $CellContext`y3, 1}, {$CellContext`x4, $CellContext`y4, 1}}] == 0}, {$CellContext`x, $CellContext`y}]}, With[{$CellContext`LineIntX = FullSimplify[ Part[ Flatten[ ReplaceAll[{$CellContext`x, $CellContext`y}, \ $CellContext`LineIntersection]], 1]], $CellContext`LineIntY = FullSimplify[ Part[ Flatten[ ReplaceAll[{$CellContext`x, $CellContext`y}, \ $CellContext`LineIntersection]], 2]]}, If[ And[$CellContext`LineIntersection != {}, And[ Or[ Or[ And[ Positive[ FullSimplify[$CellContext`x1 - \ $CellContext`LineIntX]], Positive[ FullSimplify[$CellContext`LineIntX - \ $CellContext`x2]]], And[ Negative[ FullSimplify[$CellContext`x1 - \ $CellContext`LineIntX]], Negative[ FullSimplify[$CellContext`LineIntX - \ $CellContext`x2]]]], Or[ And[ Positive[ FullSimplify[$CellContext`y1 - \ $CellContext`LineIntY]], Positive[ FullSimplify[$CellContext`LineIntY - \ $CellContext`y2]]], And[ Negative[ FullSimplify[$CellContext`y1 - \ $CellContext`LineIntY]], Negative[ FullSimplify[$CellContext`LineIntY - \ $CellContext`y2]]]]], Or[ Or[ And[ Positive[ FullSimplify[$CellContext`x3 - \ $CellContext`LineIntX]], Positive[ FullSimplify[$CellContext`LineIntX - \ $CellContext`x4]]], And[ Negative[ FullSimplify[$CellContext`x3 - \ $CellContext`LineIntX]], Negative[ FullSimplify[$CellContext`LineIntX - \ $CellContext`x4]]]], Or[ And[ Positive[ FullSimplify[$CellContext`y3 - \ $CellContext`LineIntY]], Positive[ FullSimplify[$CellContext`LineIntY - \ $CellContext`y4]]], And[ Negative[ FullSimplify[$CellContext`y3 - \ $CellContext`LineIntY]], Negative[ FullSimplify[$CellContext`LineIntY - \ $CellContext`y4]]]]]]], True, False]]]]]], FE`n$$2390 = 4, $CellContext`ClosedLine[ Pattern[$CellContext`x, Blank[]]] := Line[ Append[$CellContext`x, First[$CellContext`x]]], $CellContext`SimplePolygonQ[ Pattern[$CellContext`a$, Blank[List]]] := If[ And[Union[ Flatten[ With[{$CellContext`data$ = Partition[$CellContext`a$, 2, 1, 1]}, Table[ $CellContext`SegmentIntersectionQ[{ Part[$CellContext`data$, $CellContext`m], Part[$CellContext`data$, FE`n$$2390]}], {$CellContext`m, 1, Length[$CellContext`data$] - 1}, { FE`n$$2390, $CellContext`m + 1, Length[$CellContext`data$]}]]]] == { False}, $CellContext`BorderPointsCheck[$CellContext`a$] == 0], True, False], $CellContext`BorderPointsCheck[ Pattern[$CellContext`a, Blank[List]]] := Length[ Flatten[ Map[$CellContext`SegmentPoints, Partition[$CellContext`a, 2, 1, 1]], 1]] - Length[$CellContext`a] - Length[ $CellContext`BorderPoints[$CellContext`a]]}; Typeset`initDone$$ = True), SynchronousInitialization->True, UnsavedVariables:>{Typeset`initDone$$}, UntrackedVariables:>{Typeset`size$$}], "Manipulate", Deployed->True, StripOnInput->False], Manipulate`InterpretManipulate[1]]], "Output", CellGroupingRules->{"GroupTogetherGrouping", 10000.}, CellChangeTimes->{3.76077228625513*^9}, CellID->119904119] }, {10}]]