2

I need to scale a polygon.

write the following

Qt Code:

QPolygonF qpf=QPolygonF(QPolygon(4,points));
QTransform trans;
trans=trans.scale(1.5,1.5);
QPolygonF qpf2=trans.map(qpf);
path.addPolygon(qpf2);

for the points:

Qt Code:

  static const int points[8] = {
    10, 80,
    20, 10,
    80, 30,
    90, 70
    };

it generates ---15,120-- ---30,15-- ---120,45-- ---135,105--

thus it moves slightly too.

is there a way to scale from center? for example origin of the shape should be the same point after scaling. is there a built in way or must i calculate all the points again to scale? thanks

Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61
merveotesi
  • 2,145
  • 15
  • 53
  • 84

1 Answers1

3

Currently you're applying your scale with respect to the coordinates in your "World" coordinate system. Which explains the behavior you're seeing. You want to apply them in the local or "Object" coordinate system.

To achieve what you want to have you would have to translate the polygon so its center (or origin of the shape as you say) is aligned with (or rather "becomes") the origin of your coordinate system. Then you apply the scale you desire and subsequently apply the inverse of your initial translation.

Bart
  • 19,692
  • 7
  • 68
  • 77
  • thanks for detailed reply, i think i can do it with QPolygonF.translate method, i want to ask, is it appropriate to calculate my polygon's origin/center by myself? – merveotesi Aug 01 '11 at 15:15
  • Glad it helped. Btw, it seem you're new on SO and I've seen you've asked several questions. You might not be aware of this, but you might want to look at [how accepting an answer works](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). Otherwise people will start to complain at some point. Of course you only accept correct answers. Don't feel pushed into accepting anything you don't want to. Hope that helps. ;) – Bart Aug 01 '11 at 15:20
  • I don't know if Qt already provides such functionality or not, but yeah, it's perfectly reasonable to do this yourself otherwise. – Bart Aug 01 '11 at 15:21