~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: 2004-06-08 00:44:02 UTC
  • Revision ID: james.westby@ubuntu.com-20040608004402-72yu51xlh7vt6p9m
Tags: upstream-6.0pre16
ImportĀ upstreamĀ versionĀ 6.0pre16

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// --------------------------------------------------------------------
 
2
// Ipelet for creating regular k-gons
 
3
// --------------------------------------------------------------------
 
4
/*
 
5
 
 
6
    This file is part of the extensible drawing editor Ipe.
 
7
    Copyright (C) 1993-2004  Otfried Cheong
 
8
 
 
9
    Ipe is free software; you can redistribute it and/or modify it
 
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
 
12
    (at your option) any later version.
 
13
 
 
14
    As a special exception, you have permission to link Ipe with the
 
15
    CGAL library and distribute executables, as long as you follow the
 
16
    requirements of the Gnu General Public License in regard to all of
 
17
    the software in the executable aside from CGAL.
 
18
 
 
19
    Ipe is distributed in the hope that it will be useful, but WITHOUT
 
20
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
21
    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
 
22
    License for more details.
 
23
 
 
24
    You should have received a copy of the GNU General Public License
 
25
    along with Ipe; if not, you can find it at
 
26
    "http://www.gnu.org/copyleft/gpl.html", or write to the Free
 
27
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
28
 
 
29
*/
 
30
 
 
31
#include "ipelet.h"
 
32
#include "ipepath.h"
 
33
#include "ipepage.h"
 
34
#include "ipevisitor.h"
 
35
 
 
36
// --------------------------------------------------------------------
 
37
 
 
38
class KGonIpelet : public Ipelet {
 
39
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);
 
43
};
 
44
 
 
45
// --------------------------------------------------------------------
 
46
 
 
47
void KGonIpelet::Run(int, IpePage *page, IpeletHelper *helper)
 
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;
 
55
  }
 
56
 
 
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;
 
63
  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;
 
75
  double alpha = 2.0 * IpePi / k;
 
76
  IpeVector v0 = center + radius * IpeVector(1,0);
 
77
  for (int i = 0; i < k; ++i) {
 
78
    IpeVector v1 = center + radius * IpeVector(IpeAngle(i * alpha));
 
79
    sp->AppendSegment(v0, v1);
 
80
    v0 = v1;
 
81
  }
 
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");
 
88
}
 
89
 
 
90
// --------------------------------------------------------------------
 
91
 
 
92
IPELET_DECLARE Ipelet *NewIpelet()
 
93
{
 
94
  return new KGonIpelet;
 
95
}
 
96
 
 
97
// --------------------------------------------------------------------