1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

XIAO ESP32C6+SSD1306、円を塗り潰して遊ぶ。

Last updated at Posted at 2025-04-22

参考

いろいろ、注意

  • かころぐをみよ
  • なぜか?アルゴリズム系は、アクセスが多い(愛好者がいる?)(パクリだけど、やや、ヒット)
  • 例題の為の例題
  • 1ビットの固定小数点を使っている(ある意味、今回のメインテーマ)
  • なぜか、各CPUにほぼ、固定小数点系の命令は、付いているが、あまり、使われていない。(各言語でマクロレベル(なんの事))(保守性や互換性が下がる?)

目的

ピタゴラスの定理を使って円を塗りつぶす
c x c = a x a + b x b

結果

o_coq878.jpg

プログラム




//OLED_SSD1331_enko_XIAO_ESP32C6


//ヘッダーファイル
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>


//定義
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);



void draw_circle_bad(int diameter) {

    // 円の中心の座標
    int cx = diameter / 2;
    int cy = diameter / 2;
    // 円の半径
    int radius = diameter / 2;
    // 円を描画する

  for(int y = 0;y < diameter;y++){
    for(int x = 0;x < diameter;x++){


      // 円の中心からの距離を計算・判定する
      // 0.5 を足し、ピクセルの中心で判定する
      int dx = (x<<1) + 1 - (cx<<1);
      int dy = (y<<1) + 1 - (cy<<1);

      if (  ((dx * dx) + (dy * dy)) <= ((radius<<1) * (radius<<1))  ) {
        // 円の中なので、点を描画する
        display.drawPixel(x,y,WHITE);
      }

    }
    delay(50);
    display.display();
  }
  
    // 描画バッファの内容を画面に表示
  display.display();

}


//初期化
void setup() {

  //I2Cの初期化
  Wire.begin(D9,D10); //XIAO ESP32C6+SSD1306
  //Wire.begin(D10,D9); //XIAO ESP32C6+GROVE

  // I2Cアドレスは使用するディスプレイに合わせて変更する
  display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);

}//setup


//メインループ
void loop() {

  // 画面表示をクリア
  display.fillScreen(BLACK);

  draw_circle_bad(30);

  delay(5000); //5秒待つ

}//loop



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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?