Triangle Containment
Three distinct points is plotted at random on a Cartesian plane, for which-1000≤x, y≤1000, such that a triangle is F Ormed.
Consider the following and the triangles:
A ( -340,495), B ( -153,-910), C (835,-947) X ( -175,41), Y ( -421,-714), Z (574,-645)
It can be verified this triangle ABC contains the origin, whereas Triangle XYZ does not.
Using Triangles.txt (Right click and ' Save link/target as ... '), a 27K text file containing the co-ordinates of one thousand "Random" triangles, find the number of triangles for which the interior contains the origin.
Note:the first and examples in the file represent the triangles in the example given above.
The triangle containing the origin.
Three different points are randomly selected from the Cartesian plane, and their coordinates meet -1000≤X, y≤1000, and these three points form a triangle.
Consider the following two triangles:
A ( -340,495), B ( -153,-910), C (835,-947) X ( -175,41), Y ( -421,-714), Z (574,-645)
You can verify that the triangle ABC contains the origin, and Triangle XYZ does not contain the origin point.
In the 27K text file Triangles.txt (right-click and select "Save Target as ...") contains the coordinates of 1000 "random" triangles to find out the number of triangles that contain the origin in its interior.
Note: The first two triangles in the file are the above examples.
Solving
Consider the origin of the triangle inside the triangles, the origin to two sides of the angle should have two obtuse angles, but found that the result is 510, that is wrong, I am based on the cosine theorem Costheta there may be more than 90 degrees of problems in practice but in my calculations, I do not know how to judge. Another problem is that I do not know whether this idea is a problem, the following procedure is not right, stay here to change.
PackageLevel4;ImportJava.io.BufferedReader;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileReader;Importjava.io.IOException;Importjava.util.ArrayList; Public classpe0102{ Public Static voidrun () {intCount = 0; ArrayList<int[]> list =ReadData (); for(intI=0;i<list.size (); i++){ intArr[] =List.get (i); if(Iscontainmenttriangle (arr)) {count+=1; } if(i<10) System.out.println (Iscontainmenttriangle (arr)); } System.out.println (count); } //determine if the origin is inside the triangle. Public Static BooleanIscontainmenttriangle (int[] arr) { intCount =0; for(inti=0;i<arr.length-1;i+=2){ intX1 =Arr[i]; intY1 = arr[i+1]; for(intj=i+2;j<arr.length-1;j+=2){ intx2 =Arr[j]; inty2 = arr[j+1]; if(Isobtuseangle (x1,y1,x2,y2)) Count++; if(Count ==2) return true; } } return false; } //Is it obtuse? Public Static BooleanIsobtuseangle (intX1,intY1,intX2,inty2) { LongCostheta = x1*y1 + x2*Y2; if(Costheta <0) return true; return false; } //convert to Integer array Public Static int[] Stringtoint (string[] strarr) {int[] Intarr =New int[Strarr.length]; for(inti=0;i<strarr.length;i++) Intarr[i]=Integer.parseint (Strarr[i]); returnintarr; } //reading Data Public Staticarraylist<int[]>ReadData () {String filename= "Src/level4/p102_triangles.txt"; ArrayList<int[]> list =Newarraylist<int[]>(); Try{BufferedReader BufferedReader=NewBufferedReader (Newfilereader (filename)); String Line= ""; while((Line=bufferedreader.readline ())! =NULL) {string[] Strarr= Line.split (","); List.add (Stringtoint (Strarr)); } } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } returnlist; } Public Static voidMain (string[] args) {LongT0 =System.currenttimemillis (); Run (); LongT1 =System.currenttimemillis (); Longt = T1-t0; System.out.println ("Running Time=" +t/1000+ "s" +t%1000+ "MS"); }}
Java Code
The Mathblog refers to the solution in the same way that the Triangle area is equal, ABC = ABO + ACO +bco
Here we know how the three points of the triangle, according to these three points for the area, see the following solution, according to two vectors can quickly find the area of the triangle of the vector s= vector cross multiplication difference of the absolute value of One-second
The wiki has instructions
"Picture has failed to upload"
Java
PackageLevel4;ImportJava.io.BufferedReader;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileReader;Importjava.io.IOException;Importjava.util.ArrayList; Public classpe0102{ Public Static voidrun () {intCount = 0; ArrayList<int[]> list =ReadData (); for(intI=0;i<list.size (); i++){ intArr[] =List.get (i); if(Iscontainmenttria (arr)) {count+=1; }} System.out.println (count);//228//running Time=0s16ms } //determine if the origin is inside the triangle. Public Static BooleanIscontainmenttria (int[] arr) { intArea = 0; intCount =0; intX1 = arr[0]-arr[2]; intY1 = arr[1]-arr[3]; intX2 = arr[4]-arr[2]; intY2 = arr[5]-arr[3]; intAREA2 =Area (X1,Y1,X2,Y2); for(inti=0;i<arr.length-1;i+=2){ intX1 =Arr[i]; intY1 = arr[i+1]; for(intj=i+2;j<arr.length-1;j+=2){ intx2 =Arr[j]; inty2 = arr[j+1]; Area+=Area (X1,Y1,X2,Y2); } } if(Area = =area2)return true; return false; } //it's twice times the size. Public Static intAreaintX1,intY1,intX2,intY2) { intArea = Math.Abs (x1*y2-x2*Y1); returnArea ; } //convert to Integer array Public Static int[] Stringtoint (string[] strarr) {int[] Intarr =New int[Strarr.length]; for(inti=0;i<strarr.length;i++) Intarr[i]=Integer.parseint (Strarr[i]); returnintarr; } //reading Data Public Staticarraylist<int[]>ReadData () {String filename= "Src/level4/p102_triangles.txt"; ArrayList<int[]> list =Newarraylist<int[]>(); Try{BufferedReader BufferedReader=NewBufferedReader (Newfilereader (filename)); String Line= ""; while((Line=bufferedreader.readline ())! =NULL) {string[] Strarr= Line.split (","); List.add (Stringtoint (Strarr)); } } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } returnlist; } Public Static voidMain (string[] args) {LongT0 =System.currenttimemillis (); Run (); LongT1 =System.currenttimemillis (); Longt = T1-t0; System.out.println ("Running Time=" +t/1000+ "s" +t%1000+ "MS"); }}
Project Euler 102:triangle Containment contains the origin of the triangle