Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 53f2909a7701a1b19bfc4fbbf89f7925d8e064fc (plain) (blame)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
 * Copyright (c) 2004 - 2010 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Andre Dietisheim - initial API and implementation
 */
package org.eclipse.emf.cdo.ui.internal.branch.geometry;

import org.eclipse.zest.layouts.dataStructures.DisplayIndependentDimension;
import org.eclipse.zest.layouts.dataStructures.DisplayIndependentRectangle;

/**
 * Holds various utiliy method that help to deal with gemoetry classes in zest .
 */
public class GeometryUtils
{

  /**
   * Adds the width and height of the given second (source) dimension to the given first (target) dimension .
   * 
   * @param targetDimension
   *          the this dimension
   * @param sourceDimension
   *          the that dimension
   */
  public static void union(DisplayIndependentDimension targetDimension, DisplayIndependentDimension sourceDimension)
  {
    targetDimension.width += sourceDimension.width;
    targetDimension.height += sourceDimension.height;
  }

  /**
   * Moves the given Rectangle horizontally by xOffset and vertically by yOffset and returns a new translated rectangle
   * instance.
   * 
   * @param xOffset
   *          the offset on the x axis to move the rectangle
   * @param yOffset
   *          the offset on the y axis to move the rectangle
   * @param rectangle
   *          the rectangle to translate
   * @return a new translated rectangle instance
   */
  public static DisplayIndependentRectangle translateRectangle(double xOffset, double yOffset,
      DisplayIndependentRectangle rectangle)
  {
    DisplayIndependentRectangle newRectangle = new DisplayIndependentRectangle(rectangle);
    newRectangle.x += xOffset;
    newRectangle.y += yOffset;
    return newRectangle;
  }

  /**
   * Moves the given Rectangle horizontally and vertically by the the offsets indicated in the translation and returns a
   * new translated rectangle instance.
   * 
   * @param rectangle
   *          the rectangle to translate
   * @param translation
   *          the translation with the offsets on the x- and y-axis
   * @return a new translated rectangle instance
   */
  public static DisplayIndependentRectangle translateRectangle(DisplayIndependentDimension translation,
      DisplayIndependentRectangle rectangle)
  {
    return translateRectangle(translation.width, translation.height, rectangle);
  }

  /**
   * Returns the dimension needed to translate the given rectangle to the given location .
   * 
   * @param rectangleToTranslate
   *          the rectangle to translate
   * @param x
   *          the x coordinate to translate the rectangle to
   * @param y
   *          the y coordinate to translate the rectangle to
   * @return the translation
   */
  public static DisplayIndependentDimension getTranslation(DisplayIndependentRectangle rectangleToTranslate, double x,
      double y)
  {
    return new DisplayIndependentDimension(x - rectangleToTranslate.x, y - rectangleToTranslate.y);
  }

  /**
   * Gets the translation necessary to move the source coordinate to the target coordinate.
   * 
   * @param sourceCoordinate
   *          the source x
   * @param targetCoordinate
   *          the target x
   * @return the translation
   */
  public static double getTranslation(double sourceCoordinate, double targetCoordinate)
  {
    return targetCoordinate - sourceCoordinate;
  }

  /**
   * Answers whether the bottom of the first rectangle ends before the second rectangle's top starts (y coordinate).
   * 
   * @param theseBounds
   *          the first rectangle that shall end before the first one
   * @param thoseBounds
   *          the second rectangle that shall start after the first one ends
   * @return true, if successful
   */
  public static boolean bottomEndsBefore(DisplayIndependentRectangle theseBounds,
      DisplayIndependentRectangle thoseBounds)
  {
    return theseBounds.y + theseBounds.height < thoseBounds.y;
  }

  /**
   * Expands the given rectangle to to the minimum size which can hold both this Rectangle and the rectangle (x, y, w,
   * h).
   * 
   * @param x
   *          X coordinate to expand the rectangle to.
   * @param y
   *          Y coordinate to expand the rectangle to.
   * @param rectangle
   *          the rectangle to expand
   * @param width
   *          the width (starting at the x coordinate) to expand the rectangle to
   * @param height
   *          the height (starting at the y coordinate) to expand the rectangle to
   */
  public static DisplayIndependentRectangle union(DisplayIndependentRectangle rectangle, double x, double y,
      double width, double height)
  {
    DisplayIndependentRectangle bounds = new DisplayIndependentRectangle();
    double right = Math.max(rectangle.x + rectangle.width, x + width);
    double bottom = Math.max(rectangle.y + rectangle.height, y + height);
    bounds.x = Math.min(rectangle.x, x);
    bounds.y = Math.min(rectangle.y, y);
    bounds.width = right - bounds.x;
    bounds.height = bottom - bounds.y;
    return bounds;
  }

  /**
   * Expands the given first rectangle to the minimum size which can hold both this Rectangle and the second rectangle.
   * 
   * @param thisRectangle
   *          the rectangle to expand
   * @param thatRectangle
   *          the rectangle to include in the first one
   * @return the new bounds
   */
  public static DisplayIndependentRectangle union(DisplayIndependentRectangle thisRectangle,
      DisplayIndependentRectangle thatRectangle)
  {
    return union(thisRectangle, thatRectangle.x, thatRectangle.y, thatRectangle.width, thatRectangle.height);
  }

  /**
   * Scales the given rectangle by the given factors.
   * 
   * @param width
   *          the width
   * @param height
   *          the height
   * @param bounds
   *          the bounds
   */
  public static void scaleRectangle(double width, double height, DisplayIndependentRectangle bounds)
  {
    bounds.width *= width;
    bounds.height *= height;
  }

  /**
   * Subtracts the given height and width from the given rectangle and move its origin by the half of the given values.
   * 
   * @param dimension
   *          the dimension
   * @param rectangle
   *          the rectangle
   */
  public static DisplayIndependentRectangle substractBorders(DisplayIndependentDimension dimension,
      DisplayIndependentRectangle rectangle)
  {
    DisplayIndependentRectangle newRectangle = new DisplayIndependentRectangle();
    newRectangle.x = rectangle.x + dimension.width / 2;
    newRectangle.y = rectangle.y + dimension.height / 2;
    newRectangle.width = rectangle.width - dimension.width;
    newRectangle.height = rectangle.height - dimension.height;
    return newRectangle;
  }

  /**
   * Centers the given rectangle on the bounds of the given other rectangle.
   * 
   * @param rectangleToCenter
   *          the rectangle to center
   * @param rectangle
   *          the rectangle
   */
  public static void center(DisplayIndependentRectangle rectangleToCenter, DisplayIndependentRectangle rectangle)
  {
    if (rectangle == null)
    {
      rectangleToCenter.x -= rectangleToCenter.width / 2;
      rectangleToCenter.y -= rectangleToCenter.height / 2;
    }
    else
    {
      rectangleToCenter.x = rectangle.x + (rectangleToCenter.width - rectangle.width) / 2;
      rectangleToCenter.y = rectangle.y + (rectangleToCenter.height - rectangle.height) / 2;
    }
  }
}

Back to the top