0

Looking to use one variable to define an x1, x2, x3, x4 for a method. Is there a type that exists that would allow me to do this? I could just do 2 Points, but I'd rather just have one variable. If one doesn't exist, am I able to create a type similar to this that I can make useable in all classes in my package?

Thanks!

Nathan
  • 1,287
  • 6
  • 15
  • 32

3 Answers3

1

Have you considered defining your own tuple?

public class PointsTuple
{
     public Point Point1;
     public Point Point2;

     // replace with appropriate setters and getters if you want.
}

Hopefully you will give your class a more descriptive name than PointsTuple but you get the idea. Here I used public fields but if you are familiar with encapsulation you may opt to use set & get methods.


Community
  • 1
  • 1
User 12345678
  • 7,714
  • 2
  • 28
  • 46
1

Yes! The rectangle!

java.awt.rectangle

Rectangle(int x, int y, int width, int height)

Example: Rectangle r = new Rectangle(x1, y1, x1-x2, y1-y2)

Dean Leitersdorf
  • 1,303
  • 1
  • 11
  • 22
  • Ah if Rectangle was defined as (x1, y1, x2, y2) I would go for it, it looks like a custom class better suits me this time though. +1 anyways! – Nathan Jul 08 '14 at 03:19
  • Just note: The above solution (if you decide to use it) requires x1 >= x2 and y1>=y2. Else, you will be sending negative width and height. If you do not know which one is bigger, you can add Math.min/max before. – Dean Leitersdorf Jul 08 '14 at 03:23
  • @Nathan Tried that, however, Math.abs will not work in the case that x1 >= x2 and y1>=y2 is indeed true! `int x1 = 200, y1 = 200, x2 = 100, y2 = 100;` and using abs results in the rect 200,200,300,300. – Dean Leitersdorf Jul 08 '14 at 03:25
0

Try making a custom class like this:

class MyPoint{

    private int x1;
    private int x2;
    private int x3;
    private int x4;

    //Setters and Getters for the points
}
Nikhil Talreja
  • 2,754
  • 1
  • 14
  • 20