Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (769) Games in Android Showcase (230) games submitted by our members Games in WIP (856) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 Get bounding box of rotated rectangle.  (Read 1770 times) 0 Members and 1 Guest are viewing this topic.
P0jahn

Senior Devvie

Medals: 1
Projects: 3

 « Posted 2014-09-27 16:38:02 »

So, I have a rectangle with a known x, y, width and height.

The top left corner is the x:y coordinate where x is increasing while moving right and y while moving down.

I then rotate it with a known value(the rectangle is rotated around its center point):

I want to calculate the position of the red dot as well as the new width and height.
kpars
 « Reply #1 - Posted 2014-09-27 16:41:17 »

Care to elaborate?

- Jev
Longarmx
 « Reply #2 - Posted 2014-09-27 16:41:46 »

Assuming that you know all the vertices, the red dot would be at (min x, min y) (Assuming y-down orientation like you have it). The width would be max x - min x, and the height would be max y - min y.

 Games published by our own members! Check 'em out!
P0jahn

Senior Devvie

Medals: 1
Projects: 3

 « Reply #3 - Posted 2014-09-27 18:10:09 »

What are the vertices? I don't think I have them.
CopyableCougar4
 « Reply #4 - Posted 2014-09-27 21:13:12 »

If you construct rotated rectangles with my code, you construct a rectangle with topleft coordinates, dimensions, and rotation angle. Then to get each point you use the functions like x1() and y1(). You can change the angle with angle(float angleInRadians) To get your minimum/maximum values with my code, do:

 1 `float minx = Math.min(Math.min(region.x1(), region.x2()), Math.min(region.x3(), region.x4())); // region is your rotating region object`

 1 `float miny = Math.min(Math.min(region.y1(), region.y2()), Math.min(region.y3(), region.y4())); // region is your rotating region object`

 1 `float maxx = Math.max(Math.max(region.x1(), region.x2()), Math.max(region.x3(), region.x4())); // region is your rotating region object`

 1 `float maxy= Math.max(Math.max(region.y1(), region.y2()), Math.max(region.y3(), region.y4())); // region is your rotating region object`

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99  100  101  102  103  104  105  106  107  108  109  110  111  112  113  114  115  116  117  118  119  120  121  122  123  124  125  126  127  128  129  130  131  132  133  134  135  136  137  138  139  140  141  142  143  144  145  146  147  148  149  150  151  152 `package game.util;import java.awt.geom.Line2D;import static org.lwjgl.opengl.GL11.*;public class RotatingRegion extends Shape {      private Line2D.Float[] lines = new Line2D.Float[4];      public float cx, cy, originalCX, originalCY;   private float angle;   private float x, y, width, height;      public RotatingRegion(float x, float y, float width, float height) {      this(x, y, width, height, 0);   }   public RotatingRegion(float x, float y, float width, float height, float angle) {      cx = x + (width / 2);      cy = y + (height / 2);      originalCX = cx;      originalCY = cy;      this.angle = angle;      this.x = x;      this.y = y;      this.width = width;      this.height = height;      build();      for (Line2D.Float line : lines) {         addPoint(line.x1, line.y1);      }      create();   }      public float x1() {      return lines[0].x1;   }   public float x2() {      return lines[1].x1;   }   public float x3() {      return lines[2].x1;   }   public float x4() {      return lines[3].x1;   }      public float y1() {      return lines[0].y1;   }   public float y2() {      return lines[1].y1;   }   public float y3() {      return lines[2].y1;   }   public float y4() {      return lines[3].y1;   }      public float angle() {      return (float)Math.toDegrees(angle);   }      public void setCenter(float cx, float cy) {      this.cx = cx;      this.cy = cy;      x = cx - (width / 2);      y = cy - (height / 2);      build();   }      public void angle(float angle) {      this.angle = angle;      build();   }      public void build() {      float[] point1 = rotateAroundCenter(x, y, angle);      float[] point2 = rotateAroundCenter(x + width, y, angle);      float[] point3 = rotateAroundCenter(x + width, y + height, angle);      float[] point4 = rotateAroundCenter(x, y + height, angle);      lines[0] = new Line2D.Float(point1[0], point1[1], point2[0], point2[1]);      lines[1] = new Line2D.Float(point2[0], point2[1], point3[0], point3[1]);      lines[2] = new Line2D.Float(point3[0], point3[1], point4[0], point4[1]);      lines[3] = new Line2D.Float(point4[0], point4[1], point1[0], point1[1]);   }      public float[] rotateAroundCenter(float x, float y, float angle) {      float dx = x - cx;      float dy = y - cy;      float distance = (float) Math.sqrt(dx * dx + dy * dy);      float originalAngle = getAngle(cx, cy, x, y);      float newangle = originalAngle + angle;      float ndx = distance * (float)Math.sin(newangle);      float ndy = distance * (float)Math.cos(newangle);      float nx = cx + ndx;      float ny = cy + ndy;      return new float[]{ nx, ny };   }      public boolean intersects(float x, float y) {      for (int index = 0; index < lines.length; index++) {         if (lines[index].contains(x, y)) {            return true;         }      }      return false;   }      public boolean contains(float x, float y) {      int intersections = 0;      Line2D.Float test = new Line2D.Float(x - 1000, y - 1000, x, y);      for (int index = 0; index < lines.length; index++) {         if (lines[index].intersectsLine(test)) {            intersections++;         }      }      return intersections > 0 && intersections % 2 == 1;   }      public boolean contains(RotatingRegion region) {      for (int index1 = 0; index1 < region.lines.length; index1++) {         for (int index = 0; index < lines.length; index++) {            if (lines[index].intersectsLine(region.lines[index1])) {               return true;            }         }      }            return false;   }      public static float getAngle(float mousex, float mousey, float playerx, float playery) {      float deltax = mousex - playerx;      float deltay = mousey - playery;      double rawangle = Math.atan2(deltay, deltax);      float degrees = (float) Math.toDegrees(rawangle);      degrees += 90.0f; // add 90 so opengl can use it      if (degrees < 0) {         degrees += 360.0f; // fix all angles less than 0      }      if (degrees > 360.0f) {         degrees -= 360.0f; // fix all angles more than 360      }      degrees %= 360.0f;      if (degrees > 180.0f) {         degrees -= 360.0f;      }      return (float) Math.toRadians(degrees);   }}`

Hope this helps

CopyableCougar4

Either wandering the forum or programming. Most likely the latter

Github: http://github.com/CopyableCougar4
Pages: [1]
 ignore  |  Print

 EgonOlsen (1672 views) 2018-06-10 19:43:48 EgonOlsen (1720 views) 2018-06-10 19:43:44 EgonOlsen (1159 views) 2018-06-10 19:43:20 DesertCoockie (1585 views) 2018-05-13 18:23:11 nelsongames (1186 views) 2018-04-24 18:15:36 nelsongames (1714 views) 2018-04-24 18:14:32 ivj94 (2540 views) 2018-03-24 14:47:39 ivj94 (1766 views) 2018-03-24 14:46:31 ivj94 (2845 views) 2018-03-24 14:43:53 Solater (971 views) 2018-03-17 05:04:08
 Deployment and Packagingby mudlee2018-08-22 18:09:50Java Gaming Resourcesby gouessej2018-08-22 08:19:41Deployment and Packagingby gouessej2018-08-22 08:04:08Deployment and Packagingby gouessej2018-08-22 08:03:45Deployment and Packagingby philfrei2018-08-20 02:33:38Deployment and Packagingby philfrei2018-08-20 02:29:55Deployment and Packagingby philfrei2018-08-19 23:56:20Deployment and Packagingby philfrei2018-08-19 23:54:46
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org