Created
April 6, 2016 12:09
-
-
Save adamgit/e903fea022b8af439bb27760d4592595 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.geom.Rectangle2D; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.function.Consumer; | |
import java.util.function.Function; | |
public class ProcPhysicsSetVelocities | |
{ | |
public static boolean fastIntersectRectRect( Rectangle2D.Float r1, Rectangle2D.Float r2 ) | |
{ | |
return ! ( r1.x > r2.x+r2.width | |
|| r2.x > r1.x+r1.width | |
|| r1.y > r2.y+r2.height | |
|| r2.y > r1.y+r1.height ); | |
} | |
public static boolean fastIntersectRectRectNoOOP( float x1, float y1, float w1, float h1, float x2, float y2, float w2, float h2 ) | |
{ | |
return ! ( x1 > x2+w2 | |
|| x2 > x1+w1 | |
|| y1 > y2+h2 | |
|| y2 > y1+h1 ); | |
} | |
static float randomRectX() { return (float) Math.random() * 1000; } // 1000; } | |
static float randomRectY() { return (float) Math.random() * 1000; } // 1000; } | |
static float randomRectWidth() { return (float) Math.random() * 10; } | |
static float randomRectHeight() { return (float) Math.random() * 10; } | |
static Function<Integer,List<Rectangle2D.Float>> bmCreateRectangleList = (size) -> | |
{ | |
List<Rectangle2D.Float> r = new ArrayList<Rectangle2D.Float>( size ); | |
for( int i=0; i<size; i++) | |
r.add( new Rectangle2D.Float() ); | |
return r; | |
}; | |
static Consumer<List<Rectangle2D.Float>> bmRandomizeRectanglePositions = x -> | |
{ | |
for( Rectangle2D.Float r : x ) | |
{ | |
r.x = randomRectX(); | |
r.y = randomRectY(); | |
r.width = randomRectWidth(); | |
r.height = randomRectHeight(); | |
} | |
}; | |
static Function<Integer,float[]> bmCreateRectangleArrayNoOOP = (size) -> | |
{ | |
return new float[ size * 4 ]; | |
}; | |
static Consumer<float[]> bmRandomizeRectanglePositionsArrayNoOOP = x -> | |
{ | |
for( int i=0; i<x.length; i+=4 ) | |
{ | |
x[i] = randomRectX(); | |
x[i+1] = randomRectY(); | |
x[i+2] = randomRectWidth(); | |
x[i+3] = randomRectHeight(); | |
/* | |
x[i] = i%4; | |
x[i+1] = i%5 + 3; | |
x[i+2] = i%7 + 11; | |
x[i+3] = i%13;*/ | |
} | |
}; | |
public static void main( String[] args ) | |
{ | |
MBExecutableAlgorithm< List<Rectangle2D.Float>> algorithmList = new MBExecutableAlgorithm< List<Rectangle2D.Float> >() | |
{ | |
@Override | |
public void execute( List<Rectangle2D.Float> sweptLines ) | |
{ | |
for( int index = 0; index < sweptLines.size(); index++ ) | |
{ | |
Rectangle2D.Float swept = sweptLines.get( index ); | |
for( int i = 0; i < sweptLines.size(); i++ ) | |
{ | |
if( i == index ) | |
continue; | |
if( fastIntersectRectRect( swept, sweptLines.get( i ) ) ) | |
{ | |
//System.out.print( "C" ); | |
//collided = true; | |
/** BM: must do some write-back work here; sadly our rect doesn't have a field we can record that intersection collision to */ | |
sweptLines.get(i).x -= 0.1f; | |
break; | |
} | |
} | |
} | |
} | |
}; | |
MBExecutableAlgorithm<float[]> algorithmArrayNoOOP = new MBExecutableAlgorithm< float[] >() | |
{ | |
@Override | |
public void execute( float[] sweptRectsAsFloat4Vectors ) | |
{ | |
for( int index = 0; index < sweptRectsAsFloat4Vectors.length/4; index++ ) | |
{ | |
float sweptX = sweptRectsAsFloat4Vectors[ 4*index + 0 ]; | |
float sweptY = sweptRectsAsFloat4Vectors[ 4*index + 1 ]; | |
float sweptW = sweptRectsAsFloat4Vectors[ 4*index + 2 ]; | |
float sweptH = sweptRectsAsFloat4Vectors[ 4*index + 3 ]; | |
for( int i = 0; i < sweptRectsAsFloat4Vectors.length/4; i++ ) | |
{ | |
if( i == index ) | |
continue; | |
float nextX = sweptRectsAsFloat4Vectors[ 4*i + 0 ]; | |
float nextY = sweptRectsAsFloat4Vectors[ 4*i + 1 ]; | |
float nextW = sweptRectsAsFloat4Vectors[ 4*i + 2 ]; | |
float nextH = sweptRectsAsFloat4Vectors[ 4*i + 3 ]; | |
if( fastIntersectRectRectNoOOP( sweptX, sweptY, sweptW, sweptH, nextX, nextY, nextW, nextH ) ) | |
{ | |
//System.out.print( "C" ); | |
//collided = true; | |
/** BM: must do some write-back work here; sadly our rect doesn't have a field we can record that intersection collision to */ | |
sweptRectsAsFloat4Vectors[ 4*i + 0 ] -= 0.001f; | |
break; | |
} | |
} | |
} | |
} | |
}; | |
/** Do--oo-oooooo it! */ | |
//List< RunResult[] > randomizedTrials = MicroBenchmarker.measureTrialsWithWarmupsAndRanges( 0, 1000, 100 * 1000, LOOP_CHANGER.MULTIPLY, 4, bmCreateRectangleList, bmRandomizeRectanglePositions, algorithmList, 5, 5 ); | |
List< RunResult[] > randomizedTrials = MicroBenchmarker.measureTrialsWithWarmupsAndRanges( 0, 1000, 100 * 1000, LOOP_CHANGER.MULTIPLY, 4, bmCreateRectangleArrayNoOOP, bmRandomizeRectanglePositionsArrayNoOOP, algorithmArrayNoOOP, 5, 5 ); | |
MicroBenchmarker.outputConsoleCSV( randomizedTrials ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment