Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (763)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (852)
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 1692 times)
0 Members and 1 Guest are viewing this topic.
Offline 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.
Offline kpars
« Reply #1 - Posted 2014-09-27 16:41:17 »

Care to elaborate?

- Jev
Offline 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!
Legends of Yore - The Casual Retro Roguelike
Offline 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.
Offline 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) Smiley 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 Smiley

CopyableCougar4

Either wandering the forum or programming. Most likely the latter Smiley

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

 
EgonOlsen (553 views)
2018-06-10 19:43:48

EgonOlsen (657 views)
2018-06-10 19:43:44

EgonOlsen (452 views)
2018-06-10 19:43:20

DesertCoockie (792 views)
2018-05-13 18:23:11

nelsongames (1025 views)
2018-04-24 18:15:36

nelsongames (1058 views)
2018-04-24 18:14:32

ivj94 (1637 views)
2018-03-24 14:47:39

ivj94 (562 views)
2018-03-24 14:46:31

ivj94 (1451 views)
2018-03-24 14:43:53

Solater (567 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!