畫直線 水平夾角的 帶箭頭的
#include <graphics.h>
#include <math.h>
#define PI 3.14159265
static double degree_to_radian(double degree)
{
double radian = degree * PI / 180.0;
return radian;
}
static double get_distance(double start_x, double start_y, double end_x, double end_y)
{
double result, sqrt_f;
result = (start_x-end_x)*(start_x-end_x)+(start_y-end_y)*(start_y-end_y);
sqrt_f = sqrt(result);
return sqrt_f;
}
static void draw_arraw_line_imp(double s_x, double s_y, double e_x, double e_y, double length, double angle)
{
double tmp_x;
double tmp_y;
double x1;
double y1;
double x2;
double y2;
double distance = get_distance(s_x,s_y,e_x,e_y);
tmp_x = e_x + (s_x-e_x)*length/distance;
tmp_y = e_y + (s_y-e_y)*length/distance;
x1 = (tmp_x-e_x)*cos(-angle/2) - (tmp_y-e_y)*sin(-angle/2) + e_x;
y1 = (tmp_y-e_y)*cos(-angle/2) + (tmp_x-e_x)*sin(-angle/2) + e_y;
x2 = (tmp_x-e_x)*cos(angle/2) - (tmp_y-e_y)*sin(angle/2) + e_x;
y2 = (tmp_y-e_y)*cos(angle/2) + (tmp_x-e_x)*sin(angle/2) + e_y;
line(x1, y1, e_x, e_y);
line(x2, y2, e_x, e_y);
}
static draw_arraw_line(double s_x, double s_y, double e_x, double e_y)
{
line(s_x, s_y, e_x, e_y);
draw_arraw_line_imp(s_x, s_y, e_x, e_y, 15, degree_to_radian(20));
}
static void draw_arraw_line_with_angle(double start_x, double start_y, double length, double degree)
{
double delt_x, delt_y, end_x, end_y, radian;
radian = degree_to_radian(degree);
delt_x = length * cos(radian);
delt_y = length * sin(radian);
end_x = start_x + delt_x;
end_y = start_y - delt_y;
draw_arraw_line(start_x, start_y, end_x, end_y);
}
int main()
{
int driver=DETECT;
int mode;
initgraph(&driver,&mode,"d://tc");
cleardevice();
draw_arraw_line_with_angle(500, 300, 50, 30);
draw_arraw_line_with_angle(500, 300, 50, 45);
draw_arraw_line_with_angle(500, 300, 50, 90);
draw_arraw_line_with_angle(500, 300, 50, 180);
draw_arraw_line_with_angle(500, 300, 50, 270);
draw_arraw_line_with_angle(500, 300, 50, 300);
draw_arraw_line_with_angle(500, 300, 50, 10);
draw_arraw_line_with_angle(500, 300, 50, 15);
draw_arraw_line_with_angle(500, 300, 50, 20);
draw_arraw_line_with_angle(500, 300, 50, 60);
draw_arraw_line_with_angle(500, 300, 50, 80);
getch();
closegraph();
return 0;
}