弧と円の描画

Cairoでは弧、円、楕円を描くのに同じ関数: Cairo::Context::arc()が使われます。この関数は引数を5つ取ります。最初の2つは弧の中心点となる座標であり、3つ目は弧の半径です。最後の2つの引数は弧の始まる角度と終わる角度を定義します。全ての角度はラジアンであり、したがって円を描くことは弧を0から2 * M_PIラジアンまで描くのと同じことです。角度の0度は(ユーザー空間の)X軸の正の向きに、M_PI/2ラジアン(90度)は(ユーザー空間の)Y軸の正の向きにあたります。角度の増加する方向は、X軸の正の向きからY軸の正の向きとなっています。ですからデフォルトの変換行列では、角度が増加する方向は時計回りとなります。

楕円を描くには現在の変換行列をXまたはY方向にスケールしてください。例えば、x, y, width, heightで与えられる矩形内に楕円を描くには:

context->save();
context->translate(x, y);
context->scale(width / 2.0, height / 2.0);
context->arc(0.0, 0.0, 1.0, 0.0, 2 * M_PI);
context->restore();

(注意)これはCairoの公式ドキュメントの記述と異なりますがうまく動きます。

以下のコード例は描画領域に弧、円、楕円を描く単純なプログラムです。

Figure 15.4. Drawing Area - Arcs

Drawing Area - Arcs

Source Code

File: myarea.h

#ifndef GTKMM_EXAMPLE_MYAREA_H
#define GTKMM_EXAMPLE_MYAREA_H

#include <gtkmm/drawingarea.h>

class MyArea : public Gtk::DrawingArea
{
public:
  MyArea();
  virtual ~MyArea();

protected:
  //Override default signal handler:
  virtual bool on_expose_event(GdkEventExpose* event);
};

#endif // GTKMM_EXAMPLE_MYAREA_H

File: myarea.cc

#include "myarea.h"
#include <cairomm/context.h>

MyArea::MyArea()
{
}

MyArea::~MyArea()
{
}

bool MyArea::on_expose_event(GdkEventExpose* event)
{
  // This is where we draw on the window
  Glib::RefPtr<Gdk::Window> window = get_window();
  if(window)
  {
    Gtk::Allocation allocation = get_allocation();
    const int width = allocation.get_width();
    const int height = allocation.get_height();
    int lesser = MIN(width, height);

    // coordinates for the center of the window
    int xc, yc;
    xc = width / 2;
    yc = height / 2;

    Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();
    cr->set_line_width(lesser * 0.02);  // outline thickness changes
                                        // with window size

    // clip to the area indicated by the expose event so that we only redraw
    // the portion of the window that needs to be redrawn
    cr->rectangle(event->area.x, event->area.y,
            event->area.width, event->area.height);
    cr->clip();

    // first draw a simple unclosed arc
    cr->save();
    cr->arc(width / 3.0, height / 4.0, lesser / 4.0, -(M_PI / 5.0), M_PI);
    cr->close_path();   // line back to start point
    cr->set_source_rgb(0.0, 0.8, 0.0);
    cr->fill_preserve();
    cr->restore();  // back to opaque black
    cr->stroke();   // outline it

    // now draw a circle
    cr->save();
    cr->arc(xc, yc, lesser / 4.0, 0.0, 2.0 * M_PI); // full circle
    cr->set_source_rgba(0.0, 0.0, 0.8, 0.6);    // partially translucent
    cr->fill_preserve();
    cr->restore();  // back to opaque black
    cr->stroke();

    // and finally an ellipse
    double ex, ey, ew, eh;
    // center of ellipse
    ex = xc;
    ey = 3.0 * height / 4.0;
    // ellipse dimensions
    ew = 3.0 * width / 4.0;
    eh = height / 3.0;

    cr->save();

    cr->translate(ex, ey);  // make (ex, ey) == (0, 0)
    cr->scale(ew / 2.0, eh / 2.0);  // for width: ew / 2.0 == 1.0
                                    // for height: eh / 2.0 == 1.0

    cr->arc(0.0, 0.0, 1.0, 0.0, 2 * M_PI);  // 'circle' centered at (0, 0)
                                            // with 'radius' of 1.0

    cr->set_source_rgba(0.8, 0.0, 0.0, 0.7);
    cr->fill_preserve();
    cr->restore();  // back to opaque black
    cr->stroke();
  }

  return true;
}

File: main.cc

#include "myarea.h"
#include <gtkmm/main.h>
#include <gtkmm/window.h>

int main(int argc, char** argv)
{
   Gtk::Main kit(argc, argv);

   Gtk::Window win;
   win.set_title("DrawingArea");

   MyArea area;
   win.add(area);
   area.show();

   Gtk::Main::run(win);

   return 0;
}

このコード例にはいくつか注記しておくことがあります。再びこの例と前の例との唯一の違いは、on_expose_event()関数内にあります。この関数にだけ注目しましょう。また、関数の前半部分は前の例のものとほぼ同一ですからここは飛ばします。

この例ではウィンドウの高さと幅の言葉を使って、線の幅を含めほぼ全てを表現していることに注目してください。このためユーザーがウィンドウをリサイズするとウィンドウ内のものは全て合わせてスケールします。次に注目してほしいのは、関数内には描画セクションが3つあり、それぞれsave()restore()のペアで包まれているということです。こうすることで描画するごとに以前の状態に戻ることができます。

弧を描画するセクションでは新たに関数close_path()が導入されています。この関数には現在の地点から最初の地点にまでパスを戻りながら直線を描くという機能があります。close_path()を呼び出すのと、ふつうに直線をパスに沿って開始点に戻るように引くことには大きな違いがあります。close_path()を使った場合、直線同士がうまく結合されます。しかしline_to()の場合、直線は全て同じ開始点と終点を持ちますがCairoによって結合されることはありません。

[Note] 反時計回りの描画

関数Cairo::Context::arc_negative()は、Cairo::Context::arc()と角度の向きが逆という以外、全く同じものです。