~ubuntu-branches/ubuntu/precise/ipe/precise

« back to all changes in this revision

Viewing changes to src/ipelets/kgon/kgon.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Steve M. Robbins
  • Date: 2009-12-11 21:22:35 UTC
  • mfrom: (4.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20091211212235-5iio4nzpra64snab
Tags: 7.0.10-1
* New upstream.  Closes: #551192.
  - New build-depends: libcairo2-dev, liblua5.1-0-dev, gsfonts
  - patches/config.diff: Remove.  Upstream build system replaced.
  - Runtime lib package changed to libipe7.0.10 from libipe1c2a
  - Devel package renamed to libipe-dev (from libipe1-dev)
  - Package ipe depends on lua5.1 due to ipe-update-master.

* rules: Re-write to use dh.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
/*
5
5
 
6
6
    This file is part of the extensible drawing editor Ipe.
7
 
    Copyright (C) 1993-2007  Otfried Cheong
 
7
    Copyright (C) 1993-2009  Otfried Cheong
8
8
 
9
9
    Ipe is free software; you can redistribute it and/or modify it
10
10
    under the terms of the GNU General Public License as published by
11
 
    the Free Software Foundation; either version 2 of the License, or
 
11
    the Free Software Foundation; either version 3 of the License, or
12
12
    (at your option) any later version.
13
13
 
14
14
    As a special exception, you have permission to link Ipe with the
31
31
#include "ipelet.h"
32
32
#include "ipepath.h"
33
33
#include "ipepage.h"
34
 
#include "ipevisitor.h"
 
34
 
 
35
using namespace ipe;
35
36
 
36
37
// --------------------------------------------------------------------
37
38
 
38
39
class KGonIpelet : public Ipelet {
39
40
public:
40
 
  virtual int IpelibVersion() const { return IPELIB_VERSION; }
41
 
  virtual const char *Label() const { return "Regular k-gon"; }
42
 
  virtual void Run(int, IpePage *page, IpeletHelper *helper);
 
41
  virtual int ipelibVersion() const { return IPELIB_VERSION; }
 
42
  virtual bool run(int, IpeletData *data, IpeletHelper *helper);
43
43
};
44
44
 
45
45
// --------------------------------------------------------------------
46
46
 
47
 
void KGonIpelet::Run(int, IpePage *page, IpeletHelper *helper)
 
47
bool KGonIpelet::run(int, IpeletData *data, IpeletHelper *helper)
48
48
{
49
 
  IpePage::iterator it = page->PrimarySelection();
50
 
  if (it == page->end() || !it->Object()->AsPath() ||
51
 
      it->Object()->AsPath()->NumSubPaths() > 1 ||
52
 
      it->Object()->AsPath()->SubPath(0)->Type() != IpeSubPath::EEllipse) {
53
 
    helper->Message("Primary selection is not a circle");
54
 
    return;
 
49
  Page *page = data->iPage;
 
50
  int sel = page->primarySelection();
 
51
  if (sel < 0) {
 
52
    helper->message("No selection");
 
53
    return false;
 
54
  }
 
55
  const Path *p = page->object(sel)->asPath();
 
56
  if (p == 0 || p->shape().countSubPaths() != 1 ||
 
57
      p->shape().subPath(0)->type() != SubPath::EEllipse) {
 
58
    helper->message("Primary selection is not a circle");
 
59
    return false;
55
60
  }
56
61
 
57
 
  IpeString str;
58
 
  if (!helper->GetString("Enter k (number of corners)", str))
59
 
    return;
60
 
  IpeLex lex(str);
61
 
  int k;
62
 
  lex >> k;
 
62
  String str;
 
63
  if (!helper->getString("Enter k (number of corners)", str))
 
64
    return false;
 
65
  int k = Lex(str).getInt();
63
66
  if (k < 3 || k > 1000)
64
 
    return;
65
 
 
66
 
  const IpePath *p = it->Object()->AsPath();
67
 
  const IpeEllipse *e = p->SubPath(0)->AsEllipse();
68
 
  IpeMatrix m = p->Matrix() * e->Matrix();
69
 
 
70
 
  IpeVector center = m.Translation();
71
 
  IpeVector v = m * IpeVector(1,0);
72
 
  double radius = (v - center).Len();
73
 
 
74
 
  IpeSegmentSubPath *sp = new IpeSegmentSubPath;
 
67
    return false;
 
68
 
 
69
  const Ellipse *e = p->shape().subPath(0)->asEllipse();
 
70
  Matrix m = p->matrix() * e->matrix();
 
71
 
 
72
  Vector center = m.translation();
 
73
  Vector v = m * Vector(1,0);
 
74
  double radius = (v - center).len();
 
75
 
 
76
  Curve *sp = new Curve;
75
77
  double alpha = 2.0 * IpePi / k;
76
 
  IpeVector v0 = center + radius * IpeVector(1,0);
 
78
  Vector v0 = center + radius * Vector(1,0);
77
79
  for (int i = 1; i < k; ++i) {
78
 
    IpeVector v1 = center + radius * IpeVector(IpeAngle(i * alpha));
79
 
    sp->AppendSegment(v0, v1);
 
80
    Vector v1 = center + radius * Vector(Angle(i * alpha));
 
81
    sp->appendSegment(v0, v1);
80
82
    v0 = v1;
81
83
  }
82
 
  sp->SetClosed(true);
83
 
  IpePath *obj = new IpePath(helper->Attributes());
84
 
  obj->AddSubPath(sp);
85
 
  page->push_back(IpePgObject(IpePgObject::ESecondary,
86
 
                              helper->CurrentLayer(), obj));
87
 
  helper->Message("Created regular k-gon");
 
84
  sp->setClosed(true);
 
85
  Shape shape;
 
86
  shape.appendSubPath(sp);
 
87
  Path *obj = new Path(data->iAttributes, shape);
 
88
  page->append(ESecondarySelected, data->iLayer, obj);
 
89
  helper->message("Created regular k-gon");
 
90
  return true;
88
91
}
89
92
 
90
93
// --------------------------------------------------------------------
91
94
 
92
 
IPELET_DECLARE Ipelet *NewIpelet()
 
95
IPELET_DECLARE Ipelet *newIpelet()
93
96
{
94
97
  return new KGonIpelet;
95
98
}