Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Graphiti » Resizing of a Polygon - Bug?
Resizing of a Polygon - Bug? [message #698965] Wed, 20 July 2011 14:57 Go to next message
Joerg Reichert is currently offline Joerg ReichertFriend
Messages: 80
Registered: July 2009
Location: Leipzig
Member
Hello,

I am trying to resize a polygon figure following the suggestion made here:
http://www.eclipse.org/forums/index.php/mv/msg/174043/554130/#msg_554130

I encounter a strange behavior using the |> icon at figure to resize only the width. In the resizeShape method also the height of the shape had changed, so the figure gets lower and lower with each width resize, when pulling the figure wider as well as when narrowing the shape. How much lower seems to depend on the original height. For 208 the next height is 170, so factor 0.817, for 41 next is 37, so factor 0.87, when 30 it is factor 0.9.



@Override
public void resizeShape(IResizeShapeContext context) {
super.resizeShape(context);
double width = context.getWidth();
double height = context.getHeight(); // here the height has changed
...
}



In the add feature I use this code to create the hexagon figure:

ContainerShape hierarchyShape = peService.createContainerShape(diagram,
true);
int textWidth = MetaBIStyleUtil.getTextWidth(hierarchy.getName());
int textHeight = 30;
int hexagonLonger = 10;
int hexagonHeighter = 5;
int hexagonHeight = textHeight + (2 * hexagonHeighter);
int hexagonMiddleHeight = Math.round(hexagonHeight / 2);
int hexagonLongEdge = IMAGE_WIDTH + textWidth + (2 * hexagonLonger);
int hexagonShortEdge = IMAGE_WIDTH + textWidth;
int hexagonStartXLong = 5;
int hexagonStartXShort = 5 + hexagonLonger;
int hexagonStartY = 5;

Point upperLeftCorner = StylesFactory.eINSTANCE.createPoint();
upperLeftCorner.setX(hexagonStartXShort);
upperLeftCorner.setY(hexagonStartY);

Point upperRightCorner = StylesFactory.eINSTANCE.createPoint();
upperRightCorner.setX(hexagonStartXShort + hexagonShortEdge);
upperRightCorner.setY(hexagonStartY);

Point middleRightCorner = StylesFactory.eINSTANCE.createPoint();
middleRightCorner.setX(hexagonStartXLong + hexagonLongEdge);
middleRightCorner.setY(hexagonStartY + hexagonMiddleHeight);

Point lowerRightCorner = StylesFactory.eINSTANCE.createPoint();
lowerRightCorner.setX(hexagonStartXLong + hexagonLongEdge);
lowerRightCorner.setY(hexagonStartY + hexagonHeight);

Point lowerLeftCorner = StylesFactory.eINSTANCE.createPoint();
lowerLeftCorner.setX(hexagonStartXLong);
lowerLeftCorner.setY(hexagonStartY + hexagonHeight);

Point middleLeftCorner = StylesFactory.eINSTANCE.createPoint();
middleLeftCorner.setX(hexagonStartXLong);
middleLeftCorner.setY(hexagonStartY + hexagonMiddleHeight);

Collection<Point> points = new ArrayList<Point>();
points.add(upperLeftCorner);
points.add(upperRightCorner);
points.add(middleRightCorner);
points.add(lowerRightCorner);
points.add(lowerLeftCorner);
points.add(middleLeftCorner);

Polygon hierarchyHexagon = gaService.createPolygon(hierarchyShape,
points);

(I actually let start the figure in (5,5), when starting in (0, 0) the problem is not so obvious (as the y coord is 0 and so multiplication has no effect) but it still exists).

I do not if this is a bug in GEF or Graphiti.

Joerg



--
Need professional support for Xtext and EMF?
Go to: http://xtext.itemis.com
(no subject) [message #699373 is a reply to message #698965] Thu, 21 July 2011 12:57 Go to previous messageGo to next message
Michael Wenz is currently offline Michael WenzFriend
Messages: 1931
Registered: July 2009
Location: Walldorf, Germany
Senior Member
That's appears kind of strange but I'll try to explain.

The outermost points of a graphics algorithm define its size. In the case of
your ploygon the outermost points. So if you start in 0,0 and have a point
in 100,100 the size will be 100,100 (width,height). If you start in 5,5 the
size will be 95,95.

My quickly written resize algorithm assumes that your shape starts at 0,0,
otherwise you would have to add your offsets (5,5 in the upper example) to
what you get as size information from the polygon. So calculating the resize
factor using

float scaleX = new Float(width + 5) / new Float(maxX);
float scaleY = new Float(height + 5) / new Float(maxY);

should fix that in your case (conversion to float additionally fixes general
rounding issues).

Let me know if that helps or if I'm on the wrong track...
Michael


"Joerg Reichert" schrieb im Newsbeitrag
news:j06pp8$nde$1@news.eclipse.org...

Hello,

I am trying to resize a polygon figure following the suggestion made here:
http://www.eclipse.org/forums/index.php/mv/msg/174043/554130/#msg_554130

I encounter a strange behavior using the |> icon at figure to resize only
the width. In the resizeShape method also the height of the shape had
changed, so the figure gets lower and lower with each width resize, when
pulling the figure wider as well as when narrowing the shape. How much lower
seems to depend on the original height. For 208 the next height is 170, so
factor 0.817, for 41 next is 37, so factor 0.87, when 30 it is factor 0.9.



@Override
public void resizeShape(IResizeShapeContext context) {
super.resizeShape(context);
double width = context.getWidth();
double height = context.getHeight(); // here the height has changed
...
}



In the add feature I use this code to create the hexagon figure:

ContainerShape hierarchyShape = peService.createContainerShape(diagram,
true);
int textWidth = MetaBIStyleUtil.getTextWidth(hierarchy.getName());
int textHeight = 30;
int hexagonLonger = 10;
int hexagonHeighter = 5;
int hexagonHeight = textHeight + (2 * hexagonHeighter);
int hexagonMiddleHeight = Math.round(hexagonHeight / 2);
int hexagonLongEdge = IMAGE_WIDTH + textWidth + (2 * hexagonLonger);
int hexagonShortEdge = IMAGE_WIDTH + textWidth;
int hexagonStartXLong = 5;
int hexagonStartXShort = 5 + hexagonLonger;
int hexagonStartY = 5;

Point upperLeftCorner = StylesFactory.eINSTANCE.createPoint();
upperLeftCorner.setX(hexagonStartXShort);
upperLeftCorner.setY(hexagonStartY);

Point upperRightCorner = StylesFactory.eINSTANCE.createPoint();
upperRightCorner.setX(hexagonStartXShort + hexagonShortEdge);
upperRightCorner.setY(hexagonStartY);

Point middleRightCorner = StylesFactory.eINSTANCE.createPoint();
middleRightCorner.setX(hexagonStartXLong + hexagonLongEdge);
middleRightCorner.setY(hexagonStartY + hexagonMiddleHeight);

Point lowerRightCorner = StylesFactory.eINSTANCE.createPoint();
lowerRightCorner.setX(hexagonStartXLong + hexagonLongEdge);
lowerRightCorner.setY(hexagonStartY + hexagonHeight);

Point lowerLeftCorner = StylesFactory.eINSTANCE.createPoint();
lowerLeftCorner.setX(hexagonStartXLong);
lowerLeftCorner.setY(hexagonStartY + hexagonHeight);

Point middleLeftCorner = StylesFactory.eINSTANCE.createPoint();
middleLeftCorner.setX(hexagonStartXLong);
middleLeftCorner.setY(hexagonStartY + hexagonMiddleHeight);

Collection<Point> points = new ArrayList<Point>();
points.add(upperLeftCorner);
points.add(upperRightCorner);
points.add(middleRightCorner);
points.add(lowerRightCorner);
points.add(lowerLeftCorner);
points.add(middleLeftCorner);

Polygon hierarchyHexagon = gaService.createPolygon(hierarchyShape,
points);

(I actually let start the figure in (5,5), when starting in (0, 0) the
problem is not so obvious (as the y coord is 0 and so multiplication has no
effect) but it still exists).

I do not if this is a bug in GEF or Graphiti.

Joerg


--
--
Need professional support for Xtext and EMF?
Go to: http://xtext.itemis.com
Re: (no subject) [message #699457 is a reply to message #699373] Thu, 21 July 2011 15:05 Go to previous message
Joerg Reichert is currently offline Joerg ReichertFriend
Messages: 80
Registered: July 2009
Location: Leipzig
Member
I do not think it is you algorithm that is wrong, but the width returned from the context.getWidth() has changed. I assume that the context.getWidth and context.getHeight returns the width resp. the height of the shape (the black line in the diagram below). When I resize the shape in the diagram by pulling the right side I would expect that as height of the shape would be return the same value as before (I had put some log statements into the code to check that), but the height gets smaller and smaller.

index.php/fa/3426/0/

The complete resize shape implementation:

@Override
	public void resizeShape(IResizeShapeContext context) {
		super.resizeShape(context);
		double width = new Double(context.getWidth());
		double height = new Double(context.getHeight());
		PictogramElement pictogramElement = context.getPictogramElement();
		Polygon polygon = (Polygon) pictogramElement.getGraphicsAlgorithm();
		EList<Point> points = polygon.getPoints();
		double maxX = 0;
		double maxY = 0;
		for (Iterator<Point> iterator = points.iterator(); iterator
				.hasNext();) {
			Point point = iterator.next();
			if (point.getX() > maxX) {
				maxX = point.getX();
			}
			if (point.getY() > maxY) {
				maxY = point.getY();
			}
		}
		double scaleX = new Double(width / maxX);
		double scaleY = new Double(height / maxY);
		if (scaleX != 1 || scaleY != 1) {
			for (Iterator<Point> iterator = points.iterator(); iterator
					.hasNext();) {
				Point point = iterator.next();
				Long newX = Math.round(point.getX() * scaleX);
				point.setX(newX.intValue());
				Long newY = Math.round(point.getY() * scaleY);
				point.setY(newY.intValue());
			}
		}


Regards
Joerg
  • Attachment: hexagon.png
    (Size: 15.89KB, Downloaded 448 times)


--
Need professional support for Xtext and EMF?
Go to: http://xtext.itemis.com
Previous Topic:Calculate minimal width of text in shape
Next Topic:Undo History
Goto Forum:
  


Current Time: Fri Apr 26 08:23:34 GMT 2024

Powered by FUDForum. Page generated in 0.03312 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top