Working in java, I was wanting to simplify a draw function (polygon creator) that I am working with. Typically, when you create a polygon, you do this:
Polygon mypoly = new Polygon();
mypoly.addPoint(x1, y1);
mypoly.addPoint(x2, y2);
mypoly.addPoint(x3, y3);
Draw.fillPolygon(g, mypoly, Color.blue);
I would like to use an image mapper to automatically give me the coordinates, so I could just copy paste them into my own function.
myCommand(x1, y1, x2, y2, x3, y3);
Each of these would go into the polygon command on the top. The problem that I am facing though is that when mypoly is created, how would it know how many points to add and where to put them?
I am trying to get myCommand to automatically add points as I add arguments, and each point to correspond with the x,y of the original polygon creation method.
Sounds like you need to make use of the builder pattern. In pseudocode:
PolygonBuilder pb = new PolygonBuilder();
pb.addPoint(1,1);
pb.addPoint(1,2);
// etc...
Polygon p = pb.newPolygon();
so the idea is that you provide the builder with a set of points, and it'll generate you the appropriate polygon. Builders are often designed with a fluent interface. Note that the builder can act like a factory and return you appropriate subclasses of Polygon (square, triangle, pentagle etc. if you so wish).
Note that you could instead provide a method that takes a variable number of arguments, using the Java varargs mechanism. e.g.
public void addPoints(Integer... args) {
// and iterate here
}
You may wish to create a Point object to define an x/y coordinate together. Otherwise the above will have to check for an even number of arguments, and those arguments won't be tied together.
You can use varargs and create the polygon dynamically by using the constructor that gets arrays of xs and ys
(Code not tested)
public Polygon createPolygon(int... points) {
if (0 != points.length % 2) {
throw new IllegalArgumentException("Must have even number of points");
}
int numOfPoints = points.length / 2;
int xs = new int[numOfPoints];
int ys = new int[numOfPoints];
for (int i=0; i < numOfPoints;i++) {
xs[i] = points[i*2];
yx[i] = points[i*2 + 1];
}
return new Polygon(xs, ys, numOfPOints);
}
Then you can invoke the method with any number of points
Polygon p = createPolygon(x1,y1,x2,y2,x3,y3);
To extend Brian Agnew's answer, it might also be worth adding a Point class which the addPoints method could take in. It could make it slightly easier to add/remove points from your polygon.
public final class Point<X,Y>{
private final X x;
private final Y y;
public Point(X x, Y y){
this.x=x;
this.y=y;
}
public X getX(){return x;}
public Y getY(){return y;}
}
Then you could have a:
public void addPoints(Point<Integer,Integer>... points){
for(Point<Integer,Integer> point:points)
//your logic
}
I think you could use a method that received a varargs (...)
You need a wrapper for each point:
class Point {
int x;
int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
The method could be:
myCommand(Point ... points)
For call
myCommand(new Point(0,0), new Point(1,1), new Point(0,1));
And for draw:
Polygon mypoly = new Polygon();
for(Point p : points)
mypoly.addPoint(p.x,p.y);
Draw.fillPolygon(g,mypoly,Color.blue);
Related
I am writing code that is meant to use one given point of a perfect right triangle to find the remaining two. We assume for this exercise that it is a triangle like so: righttriangle
The first bit of code uses the Point2D class to establish the bottom left point like so:
public Triangle(Point2D.Double bottomLeftPoint, double height, double base){
this.base = base;
this.height = height;
this.bottomLeftPoint = bottomLeftPoint;
}
public Point2D.Double getBottomLeftTrianglePoint() {
return this.bottomLeftPoint;
}
I know that mathematically, the top point of the triangle would have the same x value, but would have the y value added by the height. Also the bottom right point would have the same y value but the x value added by the base.
My question is for method purposes, how would I structure that?
Would it be something like:
public Point2D.Double getTopTrianglePoint() {
return this.bottomLeftPoint(x, y + this.height);
}
public Point2D.Double getBottomRightTrianglePoint() {
return this.bottomLeftPoint(x + this.base, y);
}
For further info, I have a separate class that is meant to test the methods with with a test triangle:
public class TriangleTest {
private Triangle triangle01;
public TriangleTest() {
this.triangle01 = new Triangle(new Point2D.Double(1, 2), 5, 3);
}
}
Any help is appreciated. Thanks!
return this.bottomLeftPoint(x, y + this.height);
Break this down, then you'll notice this doesn't make sense. this.bottomLeftPoint is a variable of type Point2D.Double. You then.. try to treat this as a method somehow. It's not. This doesn't work.
You want to create an entirely new Point2D.Double. new Point2.Double(x, y) as per usual; Thus:
return new Point2D.Double(x, y + this.height);
Except, of course, if you try this, the compiler will tell you this doesn't work either; the compiler has no idea what x means. So, what do you intend to use there? Clearly it's the x coordinate of the Point2D.Double object referenced by your this.bottomLeftPoint field. Which has a .getX() method. So:
return new Point2D.Double(bottomLeftPoint.getX(), bottomLeftPoint.getY() + height);
I am using the following method to try to find a point (coordinate) that hasn't been previously used, and isn't within the bounds of items that have previously used and coordinates.
The way it works is I am rendering "bases" (RTS top-down game), and I am creating two random variable locations for x and y. I pass these, along with the bases texture, into the following method. The method loops through a list of rectangles that are the rectangles of each previously rendered base. If the point is within any of the rectangles, the method is called again using a different set of coordinates. It does this until it finds a set that isn't within a rectangle. It then adds a new rectangle to the list at these coordinates, and returns them so the game can render a new base.
However, the bases still overlap.
Here is the method:
private Point getCoords(int x, int y, Texture t){
for (int i=bases.size()-1; i> -1; i--) {
if (bases.get(i).contains(new Point(x,y))){
x = new Random().nextInt(map.getWidth() * map.getTileWidth());
y = new Random().nextInt(map.getHeight() * map.getTileHeight());
getCoords(x, y, t);
}
}
bases.add(new Rectangle(x,y,t.getImage().getWidth(), t.getImage().getHeight()));
return new Point(x, y);
}
And here is where it is being called:
switch(ran){
default:
int x = new Random().nextInt(map.getWidth() * map.getTileWidth());
int y = new Random().nextInt(map.getHeight() * map.getTileHeight());
Point p = getCoords(x, y, temp);
map.generateBase("air", p.x, p.y);
break;
}
Any ideas what is wrong here?
Thanks
There are several problems:
Your algorithm might be overwritting good coordinates (free ones) with wrong coordinates, you dont have any condition to exit the loop/recursion if you find a good place
You are checking for if rectangle contains the point, but later you are adding a rectanble, so it may not contain the point, but the rectangle created later may collide
try this
private Point getCoords(int x, int y, Texture t){
boolean found = false;
final int width = map.getTileWidth();
final int height = map.getTileHeight();
while(!found) {
x = new Random().nextInt(map.getWidth() * width);
y = new Random().nextInt(map.getHeight() * height);
for (int i=bases.size()-1; i> -1; i--) {
if (!bases.get(i).intersects(new Rectanble(x,y, width, height))){
found = true;
} else found = false;
}
}
bases.add(new Rectangle(x,y,t.getImage().getWidth(), t.getImage().getHeight()));
return new Point(x, y);
}
*** EDIT: Im not sure if I had to use TileWidth and TileHeight or image width and image height for width and height :D
int x = new Random().nextInt(map.getWidth() * map.getTileHeight());
Maybe a bad copy paste. It may be :
int x = new Random().nextInt(map.getWidth() * map.getTileWidth());
In both codes :-D
Okay so after some playing around, I found the issue is the rectangles that are saved are saved with a fixed location which means as the map moves, the rectangles don't. The fix is to loop through each bases and get the base's map position, rather than screen position, and check against this. Also, I found i was checking for a point in a rectangle, which may be outside the rectangle but leaves my bases overlapping still. So i now check for rectangle-rectangle collision instead
I know there is an easy solution to this problem but it's driving me crazy. Why is there an error when I want to print the new Rectangle? any help appreciated!
public class Rectangle {
public Rectangle(int x, int y, int width, int length) {
x = 5;
y = 10;
width = 20;
length = 30;
Rectangle box = new Rectangle(5, 10, 20, 30);
System.out.println(new Rectangle());
}
}
There are several problems with your code. First, you may not want to instantiate a Rectangle in the constructor of Rectangle as will lead to infinite recursion. The second problem is that you are calling a constructor that does not exist.
When you write:
new Rectangle()
the Java compiler will look for a constructor in the Rectangle class that accepts no arguments. But your code does not have such a constructor. You can add one like this:
public Rectangle(){
//Your code here to instantiate a default rectangle
}
Usually a constructor is used to set the values of the instance variables in a class rather than to execute code the way you have written it. You can move those lines that are creating rectangles into a main method to test the code.
Here is some code that does what I think you want it to:
public class Rectangle
{
int x, y, width, length; //declares the class's fields
public Rectangle(int x, int y, int width, int length)
{
this.x = x; //initializes the field x to the value of the local variable x
this.y = y; //initializes the field y to the value of the local variable y
this.width = width; //initializes the field width to the value of the local variable width
this.length = length; //initializes the field length to the value of the local variable length
System.out.println(this); //prints this object. should look similar to "Rectangle#1246889"
}
}
Please take a basic java tutorial (e.g. Providing Constructors for Your Classes), it will make your life easier.
You are calling a non-parameterized/default constructor from a parameterized constructor. The JVM in this case unable to create the default constructor. Hence in this case you need to include non-parameterized constructor explicitly into your class.
public class Rectangle {
public Rectangle(int x, int y, int width, int length) {
x = 5;
y = 10;
width = 20;
length = 30;
Rectangle box = new Rectangle(5, 10, 20, 30);
System.out.println(new Rectangle());
}
public Rectangle(){}
}
This will be error free.
First, the code (as you have provided it) can not possibly compile: you haven't declared x, y, width and height as member variables (fields) of your Rectangle. E.g.
// I'm assuming you want these private and final (I would)
private final int x, y, width, height;
Alternative, for a quick hack:
int x, y, width, height;
You are also trying to call a 0-argument constructor on your println line. Your class doesn't have a 0-argument constructor; it has a 4-argument constructor. I suspect (as noted above) you really want to print this.
But that wouldn't help much, on its own, unless you add an appropriate toString method to your class. E.g.:
public String toString() {
StringBuilder sb = new StringBuilder("Rectangle: ");
sb.append("x=").append(x);
sb.append(", y=").append(y);
sb.append(", width=").append(width);
sb.append(", height=").append(height);
return sb.toString();
}
You might want to think about implementing equals() and hashCode() too, if you choose to make this class immutable (I would). You can ixquick* or duckduckgo* this - there are plenty of explanations around.
[*] They are search engines: I don't use google.
I've been trying for the past few days to make a working implementation of a virtual trackball for the user interface for a 3D graphing-like program. But I'm having trouble.
Looking at the numbers and many tests the problems seems to be the actual concatenation of my quaternions but I don't know or think so. I've never worked with quaternions or virtual trackballs before, this is all new to me. I'm using the Quaternion class supplied by JOGL. I tried making my own and it worked (or at least as far a I know) but it was a complete mess so I just went with JOGL's.
When I do not concatenate the quaternions the slight rotations I see seem to be what I want, but of course It's hard when it's only moving a little bit in any direction. This code is based off of the Trackball Tutorial on the OpenGL wiki.
When I use the Quaternion class's mult (Quaternion q) method the graph hardly moves (even less than not trying to concatenate the quaternions).
When I tried Quaternionclass'sadd (Quaternion q)` method for the fun of it I get something that at the very least rotates the graph but not in any coherent way. It spazzes out and rotates randomly as I move the mouse. Occasionally I'll get quaternions entirely filled with NaN.
In my code I will not show either of these, I'm lost with what to do with my quaternions. I know I want to multiply them because as far as I'm aware that's how they are concatenated. But like I said I've had no success, I'm assuming the screw up is somewhere else in my code.
Anyway, my setup has a Trackball class with a public Point3f projectMouse (int x, int y) method and a public void rotateFor (Point3f p1, Point3f p2), Where Point3f is a class I made. Another class called Camera has a public void transform (GLAutoDrawable g) method which will call OpenGL methods to rotate based on the trackball's quaternion.
Here's the code:
public Point3f projectMouse (int x, int y)
{
int off = Screen.WIDTH / 2; // Half the width of the GLCanvas
x = x - objx_ - off; // obj being the 2D center of the graph
y = off - objy_ - y;
float t = Util.sq(x) + Util.sq(y); // Util is a class I made with
float rsq = Util.sq(off); // simple some math stuff
// off is also the radius of the sphere
float z;
if (t >= rsq)
z = (rsq / 2.0F) / Util.sqrt(t);
else
z = Util.sqrt(rsq - t);
Point3f result = new Point3f (x, y, z);
return result;
}
Here's the rotation method:
public void rotateFor (Point3f p1, Point3f p2)
{
// Vector3f is a class I made, I already know it works
// all methods in Vector3f modify the object's numbers
// and return the new modify instance of itself
Vector3f v1 = new Vector3f(p1.x, p1.y, p1.z).normalize();
Vector3f v2 = new Vector3f(p2.x, p2.y, p2.z).normalize();
Vector3f n = v1.copy().cross(v2);
float theta = (float) Math.acos(v1.dot(v2));
float real = (float) Math.cos(theta / 2.0F);
n.multiply((float) Math.sin(theta / 2.0F));
Quaternion q = new Quaternion(real, n.x, n.y, n.z);
rotation = q; // A member that can be accessed by a getter
// Do magic on the quaternion
}
EDIT:
I'm getting closer, I found out a few simple mistakes.
1: The JOGL implementation treats W as the real number, not X, I was using X for real
2: I was not starting with the quaternion 1 + 0i + 0j + 0k
3: I was not converting the quaternion into an axis/angle for opengl
4: I was not converting the angle into degrees for opengl
Also as Markus pointed out I was not normalizing the normal, when I did I couldn't see much change, thought it's hard to tell, he's right though.
The problem now is when I do the whole thing the graph shakes with a fierceness like you would never believe. It (kinda) moves in the direction you want it to, but the seizures are too fierce to make anything out of it.
Here's my new code with a few name changes:
public void rotate (Vector3f v1, Vector3f v2)
{
Vector3f v1p = v1.copy().normalize();
Vector3f v2p = v2.copy().normalize();
Vector3f n = v1p.copy().cross(v2p);
if (n.length() == 0) return; // Sometimes v1p equals v2p
float w = (float) Math.acos(v1p.dot(v2p));
n.normalize().multiply((float) Math.sin(w / 2.0F));
w = (float) Math.cos(w / 2.0F);
Quaternion q = new Quaternion(n.x, n.y, n.z, w);
q.mult(rot);
rot_ = q;
}
Here's the OpenGL code:
Vector3f p1 = tb_.project(x1, y1); // projectMouse [changed name]
Vector3f p2 = tb_.project(x2, y2);
tb_.rotate (p1, p2);
float[] q = tb_.getRotation().toAxis(); // Converts to angle/axis
gl.glRotatef((float)Math.toDegrees(q[0]), q[1], q[2], q[3]);
The reason for the name changes is because I deleted everything in the Trackball class and started over. Probably not the greatest idea, but oh well.
EDIT2:
I can say with pretty good certainty that there is nothing wrong with projecting onto the sphere.
I can also say that as far as the whole thing goes it seems to be the VECTOR that is the problem. The angle looks just fine, but the vector seems to jump around.
EDIT3:
The problem is the multiplication of the two quaternions, I can confirm that everything else works as expected. Something goes whacky with the axis during multiplication!
The problem is the multiplication of the two quaternions, I can confirm that everything else works as expected. Something goes whacky with the axis during multiplication!
You are absolutely correct!! I just recently submitted a correct multiplication and Jogamp has accepted my change. They had incorrect multiplication on mult(quaternion).
I am sure if you get the latest jogl release, it'll have the correct mult(Quaternion)
I did it!
Thanks to this C++ implementation I was able to develop a working trackball/arcball interface. My goodness me, I'm still not certain what the problem was, but I rewrote everything and even wrote my own Quaternions class and suddenly the whole thing works. I also made a Vectors class for vectors. I had a Vector3f class before but the Quaternions and Vectors classes are full of static methods and take in arrays. To make it easy to do vector computations on quaternions and vice versa. I will link the code for those two classes below, but only the Trackball class will be show here.
I made those two classes pretty quickly this morning so if there are any mathematical errors, well, uh, oops. I only used what I needed to use and made sure they were correct. These classes are below:
Quaternions: http://pastebin.com/raxS4Ma9
Vectors: http://pastebin.com/fU3PKZB9
Here is my Trackball class:
public class Trackball
{
private static final float RADIUS_ = Screen.DFLT_WIDTH / 2.0F;
private static final int REFRESH_ = 50;
private static final float SQRT2_ = (float) Math.sqrt(2);
private static final float SQRT2_INVERSE_ = 1.0F / SQRT2_;
private int count_;
private int objx_, objy_;
private float[] v1_, v2_;
private float[] rot_;
public Trackball ()
{
v1_ = new float[4];
v2_ = new float[4];
rot_ = new float[] {0, 0, 0, 1};
}
public void click (int x, int y)
{
v1_ = project(x, y);
}
public void drag (int x, int y)
{
v2_ = project(x, y);
if (Arrays.equals(v1_, v2_)) return;
float[] n = Vectors.cross(v2_, v1_, null);
float[] o = Vectors.sub(v1_, v2_, null);
float dt = Vectors.len(o) / (2.0F * RADIUS_);
dt = dt > 1.0F ? 1.0F : dt < -1.0F ? -1.0F : dt;
float a = 2.0F * (float) Math.asin(dt);
Vectors.norm_r(n);
Vectors.mul_r(n, (float) Math.sin(a / 2.0F));
if (count_++ == REFRESH_) { count_ = 0; Quaternions.norm_r(rot_); }
float[] q = Arrays.copyOf(n, 4);
q[3] = (float) Math.cos(a / 2.0F);
rot_ = Quaternions.mul(q, rot_, rot_);
}
public float[] getAxis ()
{
return Quaternions.axis(rot_, null);
}
public float[] project (float x, float y)
{
x = RADIUS_ - objx_ - x;
y = y - objy_ - RADIUS_;
float[] v = new float[] {x, y, 0, 0};
float len = Vectors.len(v);
float tr = RADIUS_ * SQRT2_INVERSE_;
if (len < tr)
v[2] = (float) Math.sqrt(RADIUS_ * RADIUS_ - len * len);
else
v[2] = tr * tr / len;
return v;
}
}
You can see there's a lot of similarities from the C++ example. Also I'd like to note there is no method for setting the objx_ and objy_ values yet. Those are for setting the center of the graph which can be moved around. Just saying, so you don't scratch your head about those fields.
The cross-product of two normalized vectors is not normalized itself. It's length is sin(theta). Try this instead:
n = n.normalize().multiply((float) Math.sin(theta / 2.0F));
I don't know how to complete this. I'm learning by myself and I found this exercise where you have a class for a rectangle like this:
public class Rectangulo {
private int x, y, width, heigth;
public Rectangulo(int x, int y, int width, int heigth){
this.x = x;
this.y = y;
this.width = width;
this.heigth = heigth;
}
public boolean isOverlaping(Rectangulo r1, Rectangulo r2){
}
And I have to complete the method so it returns true if the 2 rectangles are overlapping / colliding or false if they aren't. Can you give me any guide to help me think this problem, or tips?
I don't have to use the intersects method.
Thanks!
If you look at the method, you're provided with two rectangle objects as arguments, r1 & r2. Each instance has its specific value for x,y, width and height. You can use these values to determine whether the two rectangles collide or overlap
For your specific doubt, you might want to read the javadoc for Rectangle.. This will help you understand how a rectangle is created in Java