You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.7 KiB
69 lines
1.7 KiB
package sig;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Comparator;
|
|
import java.util.List;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Stream;
|
|
|
|
import javax.swing.JFrame;
|
|
import sig.engine.Panel;
|
|
|
|
class Vertex{
|
|
double x,y,z;
|
|
Vertex(double x,double y,double z) {
|
|
this.x=x;
|
|
this.y=y;
|
|
this.z=z;
|
|
}
|
|
@Override
|
|
public String toString() {
|
|
return "Vertex [x=" + x + ", y=" + y + ", z=" + z + "]";
|
|
}
|
|
}
|
|
|
|
class Triangle{
|
|
Vertex a,b,c;
|
|
Triangle(Vertex a,Vertex b,Vertex c) {
|
|
this.a=a;
|
|
this.b=b;
|
|
this.c=c;
|
|
}
|
|
@Override
|
|
public String toString() {
|
|
return "Triangle [z= "+((a.z+b.z+c.z)/3f)+" ||| a=" + a + ", b=" + b + ", c=" + c + "]";
|
|
}
|
|
}
|
|
|
|
public class JavaProjectTemplate {
|
|
public static final String PROGRAM_NAME="Sig's Java Project Template";
|
|
public static void main(String[] args) {
|
|
List<Triangle> triList = new ArrayList<>();
|
|
for (int i=0;i<30;i++) {
|
|
triList.add(new Triangle(new Vertex(Math.random()*20-10,Math.random()*20-10,Math.random()*20-10),new Vertex(Math.random()*20-10,Math.random()*20-10,Math.random()*20-10),new Vertex(Math.random()*20-10,Math.random()*20-10,Math.random()*20-10)));
|
|
}
|
|
|
|
Stream<Triangle> triStream = triList.stream();
|
|
List<Triangle> newList=triStream.sorted(new Comparator<Triangle>(){
|
|
@Override
|
|
public int compare(Triangle t1, Triangle t2) {
|
|
double z1=(t1.a.z+t1.b.z+t1.c.z)/3f;
|
|
double z2=(t2.a.z+t2.b.z+t2.c.z)/3f;
|
|
return (z1<z2)?1:(z1==z2)?0:-1;
|
|
}
|
|
}).collect(Collectors.toList());
|
|
|
|
System.out.println("Old List:");
|
|
|
|
for (int i=0;i<30;i++) {
|
|
System.out.println(triList.get(i));
|
|
}
|
|
|
|
|
|
System.out.println("New List:");
|
|
|
|
for (int i=0;i<30;i++) {
|
|
System.out.println(newList.get(i));
|
|
}
|
|
}
|
|
}
|
|
|