Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics2D;
- import java.awt.event.KeyEvent;
- import java.awt.geom.Ellipse2D;
- import java.awt.geom.Line2D;
- import java.awt.geom.Rectangle2D;
- import com.gameprogblog.engine.Game;
- import com.gameprogblog.engine.GameLoop;
- import com.gameprogblog.engine.GameLoopVariable;
- import com.gameprogblog.engine.GameScreen;
- import com.gameprogblog.engine.GameState;
- import com.gameprogblog.engine.Scene;
- import com.gameprogblog.engine.Vector;
- import com.gameprogblog.engine.core.Bound2;
- import com.gameprogblog.engine.input.GameInput;
- public class CircleRectangle implements Game
- {
- public static void main( String[] args )
- {
- Game game = new CircleRectangle();
- GameLoop loop = new GameLoopVariable( 0.1f );
- GameScreen screen = new GameScreen( 640, 480, true, loop, game );
- screen.setBackground( Color.black );
- GameScreen.showWindow( screen, "CircleRectangle" );
- }
- public static final Font FONT = new Font( "Monospaced" , Font.PLAIN, 12 );
- private enum DraggingState {
- START, END, RADIUS, NONE;
- }
- private float pointRadius = 8.0f;
- private boolean playing = false;
- private Vector start;
- private Vector end;
- private Vector radiusPoint;
- private float radius;
- private Bound2 bounds;
- private DraggingState dragging;
- @Override
- public void start( Scene scene )
- {
- bounds = new Bound2( 150, 150, 490, 330 );
- start = new Vector( 50, 400 );
- end = new Vector( 320, 240 );
- radius = 40.0f;
- radiusPoint = new Vector( start.x, start.y - radius );
- dragging = DraggingState.NONE;
- playing = true;
- }
- @Override
- public void input( GameInput input )
- {
- if (input.keyDown[KeyEvent.VK_ESCAPE])
- {
- playing = false;
- }
- Vector mouse = new Vector(input.mouseX, input.mouseY);
- if (dragging != DraggingState.NONE && !input.keyDown[KeyEvent.VK_D]) {
- dragging = DraggingState.NONE;
- }
- if (dragging == DraggingState.NONE && input.keyDown[KeyEvent.VK_D]) {
- if (mouse.distance( start ) <= pointRadius) {
- dragging = DraggingState.START;
- } else if (mouse.distance( end ) <= pointRadius) {
- dragging = DraggingState.END;
- } else if (mouse.distance( radiusPoint ) <= pointRadius) {
- dragging = DraggingState.RADIUS;
- }
- }
- switch (dragging) {
- case END:
- end.set( mouse );
- break;
- case RADIUS:
- radiusPoint.set( mouse );
- radius = radiusPoint.distance( start );
- break;
- case START:
- start.set( mouse );
- radiusPoint.set( mouse );
- radiusPoint.y -= radius;
- break;
- default:
- break;
- }
- }
- @Override
- public void update( GameState state, Scene scene )
- {
- }
- @Override
- public void draw( GameState state, Graphics2D gr, Scene scene )
- {
- gr.setColor( Color.blue );
- gr.draw( new Rectangle2D.Float( bounds.left, bounds.top, bounds.getWidth(), bounds.getHeight() ) );
- gr.setColor( Color.white );
- gr.draw( new Line2D.Float( start.x, start.y, end.x, end.y ) );
- gr.setColor( Color.green );
- gr.draw( new Ellipse2D.Float( start.x - pointRadius, start.y - pointRadius, pointRadius * 2, pointRadius * 2 ) );
- gr.setColor( Color.red );
- gr.draw( new Ellipse2D.Float( end.x - pointRadius, end.y - pointRadius, pointRadius * 2, pointRadius * 2 ) );
- gr.setColor( Color.yellow );
- gr.draw( new Ellipse2D.Float( radiusPoint.x - pointRadius, radiusPoint.y - pointRadius, pointRadius * 2, pointRadius * 2 ) );
- gr.draw( new Ellipse2D.Float( start.x - radius, start.y - radius, radius * 2, radius * 2 ) );
- gr.draw( new Ellipse2D.Float( end.x - radius, end.y - radius, radius * 2, radius * 2 ) );
- /*
- * Calculations
- */
- float L = bounds.left;
- float T = bounds.top;
- float R = bounds.right;
- float B = bounds.bottom;
- float dx = end.x - start.x;
- float dy = end.y - start.y;
- // cast point along the left axis intersecting the path start->end and measure the distance between that point and the left
- float pl = dy * (L - start.x) / dx + start.y;
- float plt = T - pl;
- float plb = pl - B;
- float pr = dy * (R - start.x) / dx + start.y;
- float prt = T - pr;
- float prb = pr - B;
- float pt = dx * (T - start.y) / dy + start.x;
- float ptl = L - pt;
- float ptr = pt - R;
- float pb = dx * (B - start.y) / dy + start.x;
- float pbl = L - pb;
- float pbr = pb - R;
- gr.setColor( Color.lightGray );
- gr.setFont( FONT );
- gr.drawString( "dist from top on left side: " + plt, 10, 20 );
- gr.drawString( "dist from bottom on left side: " + plb, 10, 32 );
- gr.drawString( "dist from top on right side: " + prt, 10, 44 );
- gr.drawString( "dist from bottom on right side: " + prb, 10, 56 );
- gr.drawString( "dist from left on top side: " + ptl, 10, 68 );
- gr.drawString( "dist from right on top side: " + ptr, 10, 80 );
- gr.drawString( "dist from left on bottom side: " + pbl, 10, 92 );
- gr.drawString( "dist from right on bottom side: " + pbr, 10, 104 );
- String message = "";
- if ((plb > radius && prb > radius) ||
- (plt > radius && prt > radius) ||
- (ptl > radius && pbl > radius) ||
- (ptr > radius && pbr > radius))
- {
- message = "not intersecting";
- }
- gr.drawString( message, 10, 116 );
- }
- @Override
- public void destroy()
- {
- }
- @Override
- public boolean isPlaying()
- {
- return playing;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement