Getting center point of some coordinates?

Discussion in 'Plugin Development' started by CXdur, Sep 8, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    CXdur

    Hey, I am working on a region system for my own server. I'm not very good with coordinates systems, but I know that some of you are :)

    How would I get the center point of some coordinates or atleast an edge?
    Code:
        public synchronized Location regionLocation(Player p) {
            String pn = p.getName();
            ResultSet rs = getRegion(pn);
            Location loc = null;
            int minx = 0;
            int y = 5;
            int minz = 0;
            int maxx = 0;
            int maxz = 0;
            try {
                while (rs.next()) {
                    minx = rs.getInt("minx");
                    minz = rs.getInt("minz");
                    maxx = rs.getInt("maxx");
                    maxz = rs.getInt("maxz");
                }
                Location loc = new Location 
            }
        }
    
    I know the code isn't finished, but what I'm trying to find out is how I can get the center point of the region with these coordinates. (If it is possible :p )

    Oh and if you have any good guides/tutorials on Coordinate-systems or something please give me a link :)

    Thanks.
     
  2. Offline

    Mitizmitiz

    How many points are you talking about here? If its just two, find the averages between the x's and the z's.
    Code:
    midX = (originX + endPointX) / 2;
    midY = (originY + endPointY) / 2;
     
  3. Offline

    CXdur

    Mitizmitiz
    There are 4 points:
    Minimum X, Minimum Z, Maximum X, Maximum Z.
     
  4. Offline

    Mitizmitiz

    min(x,y)
    max(x,y)

    Each would be a point.
     
  5. Offline

    CXdur

    Mitizmitiz
    Let's say it's a square.
    I made a square with all those points, but now I am trying to find the center point of the square by doing this. I am also trying to find each edge, but I don't know how as I'm not really a math genius.
     
  6. Offline

    Mitizmitiz

    That would be a line. To find the centre of a square/rectangle you will need four points. A very simple method of doing this could be as below.

    p1(x,z)
    p2(x,z)
    p3(x,z)
    p4(x,z)

    Code:
    midX = (p1.getX() + p2.getX()) / 2;
    midY = (p1.getZ() + p3.getZ()) / 2;
    In the above example, p1 is the top left corner, p2 is the top right, p3 is the bottom left and p4 is the bottom right.
     
  7. Offline

    CXdur

    Mitizmitiz
    Well I made a nice square with minX, minZ, maxX, maxZ. But I want to find the middle of it and each of the edges. Any ideas?

    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  8. Offline

    stelar7

    (maxX + minX) / 2 would be the center x coord
    (maxZ + minZ) / 2 would be the center z coord

    the new location would then be:
    new Location (((maxX + minX) / 2), y, ((maxZ + minZ) / 2));

    which would be in the center of your square
     
  9. Offline

    CXdur

    stelar7
    Thanks, works now :)
    Code:
    public synchronized Location regionLocation(Player p) {
            String pn = p.getName();
            ResultSet rs = getRegion(pn);
            int minx = 0;
            int y = 5;
            int minz = 0;
            int maxx = 0;
            int maxz = 0;
            Location loc = null;
            try {
                while (rs.next()) {
                    minx = rs.getInt("minx");
                    minz = rs.getInt("minz");
                    maxx = rs.getInt("maxx");
                    maxz = rs.getInt("maxz");
                }
                int centerx = (maxx + minx) / 2;
                int centerz = (maxz + minz) / 2;
                loc = new Location(Bukkit.getWorld("region"), centerx, y, centerz);
                p.teleport(loc);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return loc;
        }
    
    Do you know how I can get every corner as a location?
     
  10. Offline

    stelar7

    Location loc1 = new Location(maxX, y, maxY);
    Location loc2 = new Location(minX, y, maxY);
    Location loc3 = new Location(maxX, y, minY);
    Location loc4 = new Location(minX, y, minY);
     
    CXdur likes this.
  11. Offline

    CXdur

    Thanks.
     
  12. Offline

    Zidkon

    We need more maths at schools? xD
     
  13. Offline

    stelar7

    nope, just older devs
     
Thread Status:
Not open for further replies.

Share This Page