jw_cad 外部変形 - (983) gccでレムニスケートを描く -

外部変形は データのやり取りをテキストファイルで行うので プログラム言語は 自由に選ぶことができます。図形は機能的かつシンプルなため、数多くのユーザーに受け入れられています。

 

gccレムニスケートを描く

:gccレムニスケートを描く
@echo off
if not exist %~dpn0.exe (
  for /f "delims=:" %%n in ('findstr /n "^#!" %0') do (
    more +%%n %0 > %~dpn0.c
    gcc -Os %~dpn0.c -o %~dpn0.exe -s
  )
)
%~dpn0
goto:eof

REM #jww
REM #1-%d 中心点を指示してください
REM #2%d X軸上の点を指示してください
REM #e

#!この次の行からプログラムを書いてください
#include "jw.h"

double __f__(double a, double p) {
  return a * cos(p) * sqrt(1.0 - 0.5 * pow(sin(p), 2));
}

double __g__(double a, double p) {
  return a / sqrt(2.0) * sin(p) * cos(p);
}

void h(FILE *f, int n, double x1, double y1, double x2, double y2) {
  double d, si, co, a, p, x, y, xa, ya, xb, yb;
  int i;
  d = atan2(y2 - y1, x2 - x1) + PI / 2;
  si = sin(d);
  co = cos(d);
  a = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
  p = 2 * PI / n;
  x = __f__(a, 0);
  y = __g__(a, 0);
  xa =  x * si + y * co + x1;
  ya = -x * co + y * si + y1;
  for (i = 1; i <= n; i++) {
    x = __f__(a, i * p);
    y = __g__(a, i * p);
    xb =  x * si + y * co + x1;
    yb = -x * co + y * si + y1;
    fprintf(f, "%.15g %.15g %.15g %.15g\n", xa, ya, xb, yb);
    xa = xb;
    ya = yb;
  }
}

int main(void)
{
  FILE *f;
  char S_[256], *F[20];
  double x1, y1, x2, y2;
  if ( (f = fopen("jwc_temp.txt", "r")) != NULL) {
    while (fgets(S_, 256, f) != NULL) {
      split(chomp(S_), F);
      if (strncmp(S_, "hp1", 3) == 0) { x1 = atof(F[1]); y1 = atof(F[2]); }
      if (strncmp(S_, "hp2", 3) == 0) { x2 = atof(F[1]); y2 = atof(F[2]); }
    }
  } else {
    exit(MISSING_JWC_TEMP_TXT);
  }
  fclose(f);
  f = fopen("jwc_temp.txt", "w");
  h(f, 100, x1, y1, x2, y2);
  fclose(f);
  return 0;
}