LoginSignup
1
0

More than 5 years have passed since last update.

ggezで図形を描く

Last updated at Posted at 2018-12-17

let color = Color::from((255, 0, 0, 255)); //rgba
graphics::set_color(ctx, color)?;
let start_point = graphics::Point2::new(5., 5.); // 開始座標
let end_point = graphics::Point2::new(500., 5.); // 終了座標
graphics::line(
    ctx,
    &[start_point, end_point],
    2., // 幅
)?;

指定した座標から指定した座標までの線を引く。第3引数は線の幅。
色指定は省略すると白になる。

let color = Color::from((0, 255, 0, 255)); //rgba
graphics::set_color(ctx, color)?;
graphics::circle(
    ctx,
    graphics::DrawMode::Fill, // 塗りつぶしモード
    graphics::Point2::new(100., 380.), // 中心座標
    100., // 半径
    2., // 許容誤差
 )?;

指定した座標を中心に円を描く。DrawModeにはFillとLineがある。
色指定は省略すると白になる。

楕円

let color = Color::from((0, 0, 255, 255)); //rgba
graphics::set_color(ctx, color)?;
graphics::ellipse(
    ctx,
    graphics::DrawMode::Fill, // 塗りつぶしモード
    graphics::Point2::new(600., 200.), //中心座標
    50., //x方向半径
    120., //y方向半径
    1. //許容誤差
)?;

x軸の半径とy軸の半径を指定して楕円を描く。DrawModeにはFillとLineがある。
色指定は省略すると白になる。

長方形

let color = Color::from((0, 0, 0, 255)); //rgba
graphics::set_color(ctx, color)?;
graphics::rectangle(
    ctx,
    graphics::DrawMode::Line(10.), // 線モード
    graphics::Rect::new(600., 400., 150., 100.), // x, y, w, h
)?;

x軸の長さとy軸の長さを指定して長方形を描く。DrawModeにはFillとLineがある。
色指定は省略すると白になる。

多角形

let color = Color::from((255, 255, 255, 255)); //rgba
graphics::set_color(ctx, color)?;
let vertices1 = graphics::Point2::new(300., 300.); // 頂点
let vertices2 = graphics::Point2::new(250., 350.); // 頂点
let vertices3 = graphics::Point2::new(350., 350.); // 頂点
graphics::polygon(
    ctx,
    graphics::DrawMode::Fill, // 塗りつぶしモード
    &[vertices1, vertices2, vertices3],
)?;

指定した点を順に結んで多角形を描く。DrawModeにはFillとLineがある。
色指定は省略すると白になる。

let color = Color::from((155, 155, 155, 255)); //rgba
graphics::set_color(ctx, color)?;
let point1 = graphics::Point2::new(150., 150.); // 点
let point2 = graphics::Point2::new(125., 175.); // 点
let point3 = graphics::Point2::new(175., 175.); // 点
graphics::points(
    ctx,
    &[point1, point2, point3],
    5., //点のサイズ
)?;

指定した座標に点(正方形)を描く。
色指定は省略すると白になる。

全体

extern crate ggez;

use ggez::conf;
use ggez::event;
use ggez::graphics::{self, DrawMode, Point2, Color};
use ggez::{Context, GameResult};

struct MainState {
}

impl MainState {
    fn new(_ctx: &mut Context) -> GameResult<MainState> {
        let s = MainState {};
        Ok(s)
    }
}

impl event::EventHandler for MainState {
    fn update(&mut self, _ctx: &mut Context) -> GameResult<()> {
        Ok(())
    }

    fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
        graphics::clear(ctx);

        // 線
        let color = Color::from((255, 0, 0, 255)); //rgba
        graphics::set_color(ctx, color)?;
        let start_point = graphics::Point2::new(5., 5.); // 開始座標
        let end_point = graphics::Point2::new(500., 5.); // 終了座標
        graphics::line(
            ctx,
            &[start_point, end_point],
            2., // 幅
        )?;

        // 円
        let color = Color::from((0, 255, 0, 255)); //rgba
        graphics::set_color(ctx, color)?;
        graphics::circle(
            ctx,
            graphics::DrawMode::Fill, // 塗りつぶしモード
            graphics::Point2::new(100., 380.), // 中心座標
            100., // 半径
            2., // 許容誤差
        )?;

        // 楕円
        let color = Color::from((0, 0, 255, 255)); //rgba
        graphics::set_color(ctx, color)?;
        graphics::ellipse(
            ctx,
            graphics::DrawMode::Fill, // 塗りつぶしモード
            graphics::Point2::new(600., 200.), //中心座標
            50., //x方向半径
            120., //y方向半径
            1. //許容誤差
        )?;

        // 長方形
        let color = Color::from((0, 0, 0, 255)); //rgba
        graphics::set_color(ctx, color)?;
        graphics::rectangle(
            ctx,
            graphics::DrawMode::Line(10.), // 線モード
            graphics::Rect::new(600., 400., 150., 100.), // x, y, w, h
        )?;


        // 多角形
        let color = Color::from((255, 255, 255, 255)); //rgba
        graphics::set_color(ctx, color)?;
        let vertices1 = graphics::Point2::new(300., 300.); // 頂点
        let vertices2 = graphics::Point2::new(250., 350.); // 頂点
        let vertices3 = graphics::Point2::new(350., 350.); // 頂点
        graphics::polygon(
            ctx,
            graphics::DrawMode::Fill, // 塗りつぶしモード
            &[vertices1, vertices2, vertices3],
        )?;

        // 点
        let color = Color::from((155, 155, 155, 255)); //rgba
        graphics::set_color(ctx, color)?;
        let point1 = graphics::Point2::new(150., 150.); // 点
        let point2 = graphics::Point2::new(125., 175.); // 点
        let point3 = graphics::Point2::new(175., 175.); // 点
        graphics::points(
            ctx,
            &[point1, point2, point3],
            5., //点のサイズ
        )?;


        graphics::present(ctx);
        Ok(())
    }
}

pub fn main() {
    let c = conf::Conf::new();
    let ctx = &mut Context::load_from_conf("super_simple", "ggez", c).unwrap();
    let state = &mut MainState::new(ctx).unwrap();
    event::run(ctx, state).unwrap();
}

実行結果

スクリーンショット 2018-12-17 14.39.29.png

現状(2018/12/17)でサポートされている図形はおそらくこれで全部だと思う。
graphics::Color::from();を使うことで0~255で表記できる。(graphics::Color::new()だと0~1)

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0