⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drawshapes.cpp

📁 C++&datastructure书籍源码,以前外教提供现在与大家共享
💻 CPP
字号:
#include "canvas.h"
#include "prompt.h"
#include "randgen.h"
#include "dice.h"

// fill screen with random shapes

Point getPoint(Canvas& c)
// postcondition: return a random point in Canvas c
{
    RandGen gen;
    return Point(gen.RandReal(0,c.width()), gen.RandReal(0,c.height()));
}

void drawShape(Canvas & c)
// postcondition: random shape/random size drawn on c
{
    const int NUM_SHAPES = 4;   // # different shapes
    const int MAX_SIZE = 30;    // max size of a shape
    Dice shapeDie(NUM_SHAPES);  // for randomizing selections
    Dice sizeDie(MAX_SIZE);     // for randomizing size
    
    Point p1(getPoint(c));
    Point p2(p1.x + sizeDie.Roll(), p1.y + sizeDie.Roll());
    
    switch (shapeDie.Roll())
    {
        case 1 :
          c.DrawRectangle(p1,p2);
          break;
        case 2 :
          c.DrawEllipse(p1,p2);
          break;
        case 3 :
          c.DrawCircle(p1, sizeDie.Roll());
          break;
        case 4 :
          c.DrawTriangle(p1,p2,getPoint(c));
          break;
    }    
}

int main()
{
    const int WIDTH=  200, HEIGHT= 200;
    RandGen rnd;
    Canvas c(WIDTH,HEIGHT,20,20);
    int numSquares = PromptRange("# of shapes: ",1,1000);
    int k;
    for(k=0; k < numSquares; k++)
    {   // c.SetFrame();
        c.SetColor(CanvasColor(rnd.RandInt(0,255), rnd.RandInt(0,255),
                               rnd.RandInt(0,255)));  
        drawShape(c);
    }
    c.runUntilEscape();
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -