c++绘制极坐标曲线-使用Allegro绘图库
曲线绘制使用的allegro5图形图像库很简单,它的2d功能类似MFC的GDI绘图,对于函数平面曲线都是采样曲线上的点连成多段线对曲线逼近,下面两个程序都是极坐标下的曲线,一个是阿基米德螺旋线,一个是cos曲线.curve0.cpp 阿基米德螺旋线#include <stdio.h>#include <allegro5/allegro.h>#include <alle
·
曲线绘制使用的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条内容
所有评论(0)