台形を描くプログラム
Posted: 2012年11月07日(水) 01:29
fortranかC言語で台形を描くプログラムを教えていただけませんか。
自分の求めている回答と違うと思われたならば、フォーラムルールを参照の上、疑問 さんが書きました:fortranかC言語で台形を描くプログラムを教えていただけませんか。
#include <stdio.h> // printf, scanf
#include <string.h> // memset
#include <math.h> // fabs
#define N 1024
#define H 50
#define W 79
#define M 2
char screen[H][W];
double xmin, xmax;
void clearScreen() { memset(screen, ' ', sizeof screen); }
void line(double x1, double y1, double x2, double y2)
{
double rx = (W - 2*M) / (xmax - xmin), ry = rx * 0.5;
int i;
for (i = 0; i <= N; i++) {
double x = ((N - i) * x1 + i * x2) / N;
double y = ((N - i) * y1 + i * y2) / N;
int sx = (int) ((x - xmin) * rx + 0.5) + M;
int sy = (int) (y * ry + 0.5) + M;
if (sy >= 0 && sy < H) screen[sy][sx] = '*';
}
}
void display(void)
{
int i = 50;
while (--i >= 0) printf("%.*s\n", W, screen[i]);
}
int main(void)
{
double a, b, c, h;
printf("台形OABC の 4つの頂点 O(0,0), A(a,h), B(b,h), C(c,0) に必要な値の入力\n");
printf("A の x座標 a: "); if (scanf("%lf", &a) != 1) return 1;
printf("B の x座標 b: "); if (scanf("%lf", &b) != 1) return 1;
printf("C の x座標 c: "); if (scanf("%lf", &c) != 1) return 1;
printf("台形の高さ h: "); if (scanf("%lf", &h) != 1) return 1;
xmax = xmin = 0;
if (a > xmax) xmax = a;
if (a < xmin) xmin = a;
if (b > xmax) xmax = b;
if (b < xmin) xmin = b;
if (c > xmax) xmax = c;
if (c < xmin) xmin = c;
clearScreen();
line(0,0, a,h);
line(a,h, b,h);
line(b,h, c,0);
line(0,0, c,0);
display();
return 0;
}