c++绘制极坐标曲线-使用Allegro绘图库

曲线绘制使用的allegro5图形图像库很简单,它的2d功能类似MFC的GDI绘图,对于函数平面曲线都是采样曲线上的点连成多段线对曲线逼近,下面两个程序都是极坐标下的曲线,一个是阿基米德螺旋线,一个是cos曲线.

curve0.cpp 阿基米德螺旋线

#include <stdio.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
#include <vector>

#define _USE_MATH_DEFINES
#include <math.h>

int w=640;
int h=480;

struct vec2d
{
	double x;
	double y;
};

vec2d rsi2xy(double r, double si)
{
	vec2d p;
	double s=si *M_PI/180.0;
	p.x = r*cos(s);
	p.y = r*sin(s);
	return p;
}

double agmd(double si)
{
	return 80.0+0.1*si;
}

int main(int argc, char *argv[])
{
	if (!al_init())
	{
		fprintf(stderr, "Couldn't initialize Allegro\n");
		return 1;
	}
	
	    al_set_new_display_option(
	           ALLEGRO_SAMPLE_BUFFERS, 2, 
	           ALLEGRO_SUGGEST);
        al_set_new_display_option(
               ALLEGRO_SAMPLES, 8, 
               ALLEGRO_SUGGEST);
	
	ALLEGRO_DISPLAY *display = al_create_display(w, h);
	if (display == NULL)
	{
		fprintf(stderr, "Couldn't create display\n");
		return 1;
	}
	
	w=al_get_display_width(display);
	h=al_get_display_height(display);
	
	auto colwhite=al_map_rgb(0xFF, 0xFF, 0xFF);
    auto colblack=al_map_rgb(0x00, 0x00, 0x00);
    auto colgreen=al_map_rgb(0x00, 0xFF, 0x00);
	auto colblue=al_map_rgb(0x00, 0x00, 0xFF);
	
	
	while(1)
	{
	    al_clear_to_color(colwhite);
	    
	    // draw axis
	    al_draw_line(0,h/2,w, h/2,colblack,3);
	    al_draw_line(w/2,0,w/2,h,colblack,3);
	    
	    // draw helix r= r0+a*si
	    double cx=w/2;
	    double cy=h/2;
	    double si = 0.0;
	    double dsi=5.0;
	    double simax=360.0*10+1.0;
	    double r=agmd(si);
	    vec2d  p0=rsi2xy(r,si);
	    si+=dsi;
	    while(si<simax)
	    {
	    	  double r=agmd(si);
	          vec2d  p1=rsi2xy(r,si);
	          al_draw_line(p0.x+cx,cy-p0.y,p1.x+cx,cy-p1.y,colblue,7);
	          p0=p1;
	          si+=dsi;
	    };
	   
	    al_flip_display();
	}
	al_rest(2.);
	al_destroy_display(display);
	return 0;
}

在这里插入图片描述

#include <stdio.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
#include <vector>

#define _USE_MATH_DEFINES
#include <math.h>

int w=1000;
int h=800;

struct vec2d
{
	double x;
	double y;
};

vec2d rsi2xy(double r, double si)
{
	vec2d p;
	double s=si *M_PI/180.0;
	p.x = r*cos(s);
	p.y = r*sin(s);
	return p;
}

double cospole(double si)
{
	double s=si*M_PI/180.0;
	return 400.0+60.0*cos(16.0*s);
}

int main(int argc, char *argv[])
{
	if (!al_init())
	{
		fprintf(stderr, "Couldn't initialize Allegro\n");
		return 1;
	}
	
		al_set_new_display_option(
	           ALLEGRO_SAMPLE_BUFFERS, 2, 
	           ALLEGRO_SUGGEST);
        al_set_new_display_option(
               ALLEGRO_SAMPLES, 8, 
               ALLEGRO_SUGGEST);
	
	ALLEGRO_DISPLAY *display = al_create_display(w, h);
	if (display == NULL)
	{
		fprintf(stderr, "Couldn't create display\n");
		return 1;
	}
	
	w=al_get_display_width(display);
	h=al_get_display_height(display);
	
	auto colwhite=al_map_rgb(0xFF, 0xFF, 0xFF);
    auto colblack=al_map_rgb(0x00, 0x00, 0x00);
    auto colgreen=al_map_rgb(0x00, 0xFF, 0x00);
	auto colblue=al_map_rgb(0x00, 0x00, 0xFF);
	
	
	while(1)
	{
	    al_clear_to_color(colwhite);
	    
	    // draw axis
	    al_draw_line(0,h/2,w, h/2,colblack,3);
	    al_draw_line(w/2,0,w/2,h,colblack,3);
	    
	    // draw helix r= r0+a*si
	    double cx=w/2;
	    double cy=h/2;
	    double si = 0.0;
	    double dsi=0.2;
	    double simax=360.0+1.0;
	    double r=cospole(si);
	    vec2d  p0=rsi2xy(r,si);
	    si+=dsi;
	    while(si<simax)
	    {
	    	  double r=cospole(si);
	          vec2d  p1=rsi2xy(r,si);
	          al_draw_line(p0.x+cx,cy-p0.y,p1.x+cx,cy-p1.y,colblue,7);
	          p0=p1;
	          si+=dsi;
	    };
	   
	    al_flip_display();
	}
	al_rest(2.);
	al_destroy_display(display);
	return 0;
}

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值