首页 > 编程语言 >C++ 时钟;多角星;花环实现。

C++ 时钟;多角星;花环实现。

时间:2023-06-08 11:22:15浏览次数:49  
标签:angle int 0.5 C++ radius 多角 offest 花环 radian

 

最近在回顾C++,写了几个小玩意儿练习一下,该找工作了,十分焦虑。

好了,开始正文

首先如何画多角星?比如五角星,六角星等等?

这里以五角星为例,r1需要被指定,r2可以通过正弦定理得到。然后通过每一步增加360/2n的角度,依次通过短半径和长半径循环计算每个顶点的坐标。如果需要绘制的图形堆成,那么仅需计算一半,另一半通过对称得出。直接贴代码:

void InitNStar(int n, int x, int y, float radius, float offest) {
    POINT* pts = new POINT[n * 2];
    float angle_A = 360.0 / n;
    float angle_D = 180.0 - 90.0 * (n - 2) / n;

    float angle_E = 180.0 - angle_D - angle_A / 2.0;
    float short_radius = radius / sinf(to_radian(angle_D)) * sinf(to_radian(angle_E));
    float rs[] = {radius,short_radius};
    for (int i = 0; i < 2*n; i++) {
        int sx = cosf(to_radian(270.0 + i * angle_A / 2.0 + offest)) * rs[i % 2] + 0.5;
        int sy = sinf(to_radian(270.0 + i * angle_A / 2.0 + offest)) * rs[i % 2] + 0.5;
        pts[i].x = sx+x;
        pts[i].y = sy+y;
    }
    solidpolygon(pts, n * 2);
}

 同理,对于花环的绘制依然可以参考这个思路。

如果不需要考虑每个小圆的弧度,直接绘制圆即可。

void DrawFlower(int n, int radius, int angle_C, int offest_x=0, int offest_y=0) {

    float angle_A = 2 * PI / n;
    float small_radius = radius * sinf(angle_A / 2) / sinf(to_radian(angle_C) / 2);
    float mid_radius = radius * sinf((2 * PI - to_radian(angle_C) - angle_A) / 2) / sinf(to_radian(angle_C) / 2);
    int x, y = 0;
    for (int i = 0; i < n; i++) {
        x = cosf(PI * 3 / 2 + angle_A * i) * mid_radius + 0.5;
        y = sinf(PI * 3 / 2 + angle_A * i) * mid_radius + 0.5;
        setfillcolor(RGB(253, 91 + i * (80 / n), 120 + i * (80 / n)));
        solidcircle(x + offest_x, y + offest_y, small_radius);
    }
}

然后加一点细节让他动起来:

#include <iostream>
#include <cmath>
#include <graphics.h>
#include <Windows.h>

#define PI 3.1415926

using namespace std;

float to_radian(float a) {
    return a * 3.1415926 / 180.0;
}

void DrawFlower(int n, int radius, int angle_C, int offest_x=0, int offest_y=0, int R=255,int G=91,int B=120, bool gradual=true, float offest_ang= PI * 3 / 2) {

    float angle_A = 2 * PI / n;
    float small_radius = radius * sinf(angle_A / 2) / sinf(to_radian(angle_C) / 2);
    float mid_radius = radius * sinf((2 * PI - to_radian(angle_C) - angle_A) / 2) / sinf(to_radian(angle_C) / 2);
    int x, y, x1, y1, x2, y2 = 0;
    for (int i = 0; i < n; i++) {
        x = cosf(offest_ang + angle_A * i) * mid_radius + 0.5 + offest_x;
        y = sinf(offest_ang + angle_A * i) * mid_radius + 0.5 + offest_y;
        if (gradual)
            setfillcolor(RGB(R, G + i * (80 / n), B + i * (80 / n)));
        else
            setfillcolor(RGB(R, G, B));
        /*solidcircle(x + offest_x, y + offest_y, small_radius);*/
        float ang = offest_ang + PI  - to_radian(angle_C) / 2.0 + i * angle_A;
        solidpie(x - small_radius, y - small_radius, x + small_radius, y + small_radius, -ang, -ang - to_radian(angle_C));
        x1 = cosf(offest_ang + angle_A * i - angle_A * 0.5) * (radius + 2) + 0.5 + offest_x;
        y1 = sinf(offest_ang + angle_A * i - angle_A * 0.5) * (radius + 2) + 0.5 + offest_y;
        x2 = cosf(offest_ang + angle_A * (i + 1) - angle_A * 0.5) * (radius + 2) + 0.5 + offest_x;
        y2 = sinf(offest_ang + angle_A * (i + 1) - angle_A * 0.5) * (radius + 2) + 0.5 + offest_y;
        POINT pts[4] = { {x1,y1},{x,y},{x2,y2} ,{0+offest_x,0+offest_y} };
        solidpolygon(pts, 4);
    }
}

int main() {

    initgraph(800, 1000);

    setorigin(400, 400);

    setbkcolor(RGB(135, 206, 235));
    cleardevice();
    int n = 8, i = 0;

    while (1) {
        cleardevice();
        BeginBatchDraw();
        int x = sinf(PI * i / 400) * 50;
        int y = sinf(PI * i / 400) * 10;
        POINT pts[4] = { {x,y},{100,200},{-100,400},{0,600} };
        setlinestyle(PS_SOLID | PS_ENDCAP_ROUND, 16);
        setlinecolor(RGB(0, 120, 80));
        polybezier(pts, 4);
        DrawFlower(8, 150, 120, x+3, y+3, 220, 220, 220, false, PI * i++ / 400);
        DrawFlower(8, 150, 120, x, y, 255, 92, 120, true, PI * i++ / 400);
        setfillcolor(RGB(238, 232, 171));
        solidcircle(x, y, 30);
        FlushBatchDraw();
        if (i > 800)
            i = 0;
        Sleep(10);
    }

    getchar();
    return 0;
}

 

 

 如何画时钟?

获取时分秒之后,通过时分秒的值得到指针角度,每个循环进行更新。

        time_t nowtime;
    time(&nowtime); 
    tm p;
    localtime_s(&p, &nowtime); 

    int hour = p.tm_hour;
    int minute = p.tm_min;
    int second = p.tm_sec;
    int x1, y1, x2, y2 = 0;

    // 时针
    setlinecolor(RGB(23,124,80));
    setlinestyle(PS_SOLID | PS_ENDCAP_ROUND, 16);
    x1 = int(cosf(to_radian(90 + hour * 30)) * 30 + 0.5);
    y1 = int(sinf(to_radian(90 + hour * 30)) * 30 + 0.5);
    x2 = int(cosf(to_radian(270 + hour * 30)) * 150 + 0.5);
    y2 = int(sinf(to_radian(270 + hour * 30)) * 150 + 0.5);
    line(x1, y1, x2, y2);
    //solidroundrect(-30, -8, 200, 8, 20, 10);

    //分针
    setlinecolor(RGB(23, 124, 180));
    setlinestyle(PS_SOLID | PS_ENDCAP_ROUND, 12);
    x1 = int(cosf(to_radian(90 + minute * 6)) * 35 + 0.5);
    y1 = int(sinf(to_radian(90 + minute * 6)) * 35 + 0.5);
    x2 = int(cosf(to_radian(270 + minute * 6)) * 165 + 0.5);
    y2 = int(sinf(to_radian(270 + minute * 6)) * 165 + 0.5);
    line(x1, y1, x2, y2);
    
    //秒针
    setlinecolor(BLACK);
    setlinestyle(PS_SOLID | PS_ENDCAP_ROUND, 8);
    x1 = int(cosf(to_radian(90 + second * 6)) * 40 + 0.5);
    y1 = int(sinf(to_radian(90 + second * 6)) * 40 + 0.5);
    x2 = int(cosf(to_radian(270 + second * 6)) * 180 +0.5);
    y2 = int(sinf(to_radian(270 + second * 6)) * 180 +0.5);
    line(x1, y1, x2, y2);

    setfillcolor(RGB(123, 34, 80));
    solidcircle(0, 0, 15);

最后的代码在这里:来往/EasyX_Clock (gitee.com)

 

标签:angle,int,0.5,C++,radius,多角,offest,花环,radian
From: https://www.cnblogs.com/wensi-eric/p/17464978.html

相关文章

  • C++ this 指针
    第一部分this指针的类型可理解为Box*。此时得到两个地址分别为box1和box2对象的地址。实例:#include<iostream>usingnamespacestd;classBox{public:Box(){;}~Box(){;}Box*get_address()//得到this的地址{......
  • C++ 指向类的指针
    C++指向类的指针一个指向C++类的指针与指向结构的指针类似,访问指向类的指针的成员,需要使用成员访问运算符->,就像访问指向结构的指针一样。与所有的指针一样,您必须在使用指针之前,对指针进行初始化。下面的实例有助于更好地理解指向类的指针的概念:#include<iostream>usin......
  • C++面试八股文:C和C++有哪些区别
    C++面试八股文:C和C++有哪些区别某日小二参加XXX科技公司的C++高级工程师开发岗位1面:面试官:请问C和C++的区别有哪些?小二:C++是C的超集。面试官:还有吗?小二:...面试官:面试结束,回去等消息吧。小二:淦。小二的答案对吗?实际上这句话是有问题的,严格的说,C语言和C++有很......
  • C++面试八股文:C++中,函数的参数应该传值还是传引用?
    某日二师兄参加XXX科技公司的C++工程师开发岗位第8面:面试官:C++中,函数的参数应该传值还是传引用?二师兄:要看参数的用途。如果是出参,必须传引用。如果是入参,主要考虑参数类型的大小,来决定传值还是传引用。面试官:为什么不使用指针?二师兄:传指针也称之为传引用(passbyrefereence)。......
  • 混合编程python与C++
    上个版本:只是用到ctypes进行传输,这次将python服务端更改为C++服务端,方便后续维护.本文实现功能:python传输图片给C++,C++接受图片后对图片进行处理,并将结果返回给python客户端,passimagefrompythontoC++C++服务端.h文件注意文中的model//.h#pragmaonce#in......
  • 浅谈: C++中*&的含义
    指针引用int*&A;该类型要从右往左读:变量A,是一个引用&;谁的引用呢?指针*的引用。为什么有些函数形参要用*&而不直接用*呢?......
  • C++哈希算法(一)
    哈希设计思想:试想如果我们对一个数组进行查询,这个数组里,每一个元素都是一个字符串。我们知道数组最快的检索办法是通过数组的下标进行检索,但是对于这种场景,我们无能为力,只能从头查到尾,从而查询出目标元素。如果我们要根据名字找到其中的任何一个元素,就需要遍历整个数组。最坏情......
  • 蓝桥杯十一届JavaA组-C++解题
    随便乱写,目前正确性未知C.本质上升序列#include<bits/stdc++.h>usingnamespacestd;boolaccess[4][4];intdfs(intidx,intx,inty){ if(x<0||y<0||x>=4||y>=4) return0; if(access[y][x]) return0; if(idx>=15) return1; intcount=0; access......
  • C++ 日期 & 时间
     C++标准库没有提供所谓的日期类型。C++继承了C语言用于日期和时间操作的结构和函数。为了使用日期和时间相关的函数和结构,需要在C++程序中引用<ctime>头文件。有四个与时间相关的类型:clock_t、time_t、size_t 和 tm。类型clock_t、size_t和time_t能够把系统时间......
  • C++ 引用 vs 指针
     引用很容易与指针混淆,它们之间有三个主要的不同:不存在空引用。引用必须连接到一块合法的内存。一旦引用被初始化为一个对象,就不能被指向到另一个对象。指针可以在任何时候指向到另一个对象。引用必须在创建时被初始化。指针可以在任何时间被初始化。https://www.lekaowan......