I have a my rectangle.
The application generates another rectangle.
It can be more smaller or larger than my rectangle.
How can I tell when its rect near of the mine using their X, Y, Weight and Hight?? I do not want to know if is into my rectangle.
Draw 1 or more non-visible shapes that are relative to your rectangle's position that fit your definition of "near", then check to see if these shape(s) intersect with the application-generated rectangle in question.
For example, one way you might implement this is drawing a non-visible rectangle that surrounds your rectangle, then checking to see if the surrounding rectangle intersects with the application-generated rectangle.
I found the solution!
I have calculated the middle point of my rectangle.
If the rectangle generated have into the point, is near!
You can use the Math formula to calculate the distance between two points like this:
double getDistance(int x, int y, int x2, int y2) {
double distance;
distance = Math.sqrt( Math.pow( Math.abs(x2 - x) , 2 ) + Math.pow( Math.abs(y2 - y) , 2 ) );
return distance;
}
Related
What I'm trying to do:
Create a compass when given a direction in degrees (0 - 360). Like so:
What I have done:
I have managed to get the SVG image to point in the right direction but I can't seem to get it to rotate around the circle. To attempt a solution around the circle, I have decided to try get the positioning using the ellipse tool and this formula. This is what it looks like at the moment:
(notice how the arrow faces a different direction to the ellipse, in the circle, on the axis - given the center point is the middle of the green circle)
void setDirection(float value, int radius) {
fill(secondaryColour);
float origin_x = (1280 - (width-400)/2);
float origin_y = height/2;
float x = origin_x + radius * cos(radians(value));
float y = origin_y +radius * sin(radians(value));
//grey circle
ellipse(x, y, 20, 20);
//arrow SVG
pushMatrix();
translate(200, 300);
rotate(radians(value));
scale(0.5);
shape(arrow);
popMatrix();
}
Please note: value is in degrees and radius is the radius I want the arrow to sit on. What am I doing wrong with the ellipse? and how can I bring them both together?
I found that the starting angle started on the white line, but I was assuming it would start on the red (similar to the angle of the arrow). To resolve the issue I needed to subtract 90 degrees from the variable value before converting it into radians.
i want to draw this arc in a panel that the preferedSize is set to (200,50):
i found it hard to understand what does the parameters in drawArc(). how to draw this one?
To understand the arguements read the javadoc: http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#drawArc(int,%20int,%20int,%20int,%20int,%20int)
drawArc(int x, int y, int width, int length, int startAngle, int arcAngle)
Used to draw an arc inside an imaginary rectangle whose upper left corner is at (x,y). The arc is drawn from the startAngle to startAngle + arcAngle and is measured in degrees. A startAngle of 0º points horizontally to the right (like the unit circle in math). Positive is a counterclockwise rotation starting at 0º
I'm making a game in slick2D and i wanted the game to adjust resolutions acording to the screen being used, so when i initialize my AppGameContainer i call it as the following shows
AppGameContainer appgc;
try{
appgc = new AppGameContainer(appgc.getScreenWidth(), appgc.getScreenHeight(), true);
}catch....
So my question is when using this method will i have to draw objects and do collision detection using appgc.getScreenWidth() and appgc.getScreenWidth() for example if i wanted to draw a square in the middle of the screen would i have to do it like so ?
g.fillRect(appgc.getScreenWidth, appgc.getScreenHeight(), 50, 50);
or is there some easier way around this, as this could get confusing with collision later on as opposed to just an X and a Y axis?
thanks for any helpful answers in advance.
If you want to draw a square in the middle of the screen using fillRect you have to do that :
int square_width = 50 ;
int = 50 ;
g.fillRect((appgc.getScreenWidth+square_width)/2, (appgc.getScreenHeight+square_height)/2, square_width, square_height);
Indeed there is the function :
fillRect(float x1, float y1, float width, float height)
x1 - The x coordinate of the top left corner
y1 - The y coordinate of the top left corner
width - The width of the rectangle to fill
height - The height of the rectangle to fill
In java I have three points denoting two line with making an angle. Now I have to create the angle arc about 10 pixel apart from common point. with showing angle on the arc. I am able to calculate the angle but how to draw the arc and to show the angle on the arc. Please tell me some code view or link where I can find solution for this. CODE snippet is as below.
public void paintComponent(Graphics g){
Graphics2D g2=(Graphics2D)g;
Point p1=new Point(100,100);
Point p2=new Point(200,100);
Point p3=new Point(100,0);
Line2D line1=new Line2D.Double(p1, p2);
Line2D line2=new Line2D.Double(p1, p3);
g2.draw(line1);
g2.draw(line2);
double angle=getAngle(line1,line2);
System.out.println(angle);
//g2.drawArc(110, 100, 20, 20, 100, 30);
}
public double getAngle(Line2D line1,Line2D line2){
double angle1=Math.atan2(line1.getY1()-line1.getY2(), line1.getX1()-line1.getX2());
double angle2=Math.atan2(line2.getY1()-line2.getY2(), line2.getX1()-line2.getX2());
return Math.toDegrees(angle1-angle2);
}
I don't know how to use DrawArc to draw exact arc which i want and also to put the angle on that.
Thanks & Regards.
From the documentation drawArc's arguments are:
int x, int y, int width, int height, int startAngle, int arcAngle
the x and y are your common point (p1), and your width and height are probably both 10 (to draw a circular arc with radius 10pixels)
The angle that you have computed is the last argument (arcAngle) which measure the sweep of the arc in a counter-clockwise direction. So that last part you need to work out is the start angle, which is probably your angle1 or angle2 (0 in this case is the positive x-axis or 3 o'clock position).
Keep in mind, as written you will sometimes be drawing an arc of > 180 degrees, you will need more logic if you want to always find the smallest angle between the two lines.
As for the text you can use drawString and figure out the x and y using some trigonometry with half your sweep angle and your desired radius. Though for optimal placement you might need to figure out what quadrant you are drawing in and adjust from there.
I have a huge set of points already loaded within a plane I need to draw a circle/ellipse starting from a given point and a radius distance in meters then check which points are inside the circle.
I've already done this with a polygon with the within() method, but I can't find a way to draw a circle/ellipse without having to specify every point around the polygon.
Is there a way to do this on JTS or do I need another java library?
If I understood correctly you have the radius and the center, so you can draw a circle with JTS like this:
public static Geometry createCircle(double x, double y, final double RADIUS) {
GeometricShapeFactory shapeFactory = new GeometricShapeFactory();
shapeFactory.setNumPoints(32);
shapeFactory.setCentre(new Coordinate(x, y));
shapeFactory.setSize(RADIUS * 2);
return shapeFactory.createCircle();
}
You can just verify that the distance from the point is less than the radius. No need to draw the circle to know which points are inside it. For faster run times, compare the square of the distance with the square of the radius; this saves unnecessary square root operations.
For ellipses, the problem is only slightly harder, involving a quadratic form x^2 + k y^2.
You can simply buffer the circle center with a positive value like so:
Point centerPoint = ...;
Polygon circle = (Polygon) centerPoint.buffer(0.1);