jw_cad 外部変形 - (485) 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 #1ln 基準線を指示してください
REM #e

#!この次の行からプログラムを書いてください
#include "jw.h"
int main(void)
{
  FILE *f;
  int NF;
  char S_[256], *F[20];
  double x1, y1, x2, y2;
  if ( (f = fopen("jwc_temp.txt", "r") ) != NULL) {
    while (fgets(S_, 256, f) != NULL) {
      NF = split(S_, F);
      if (strncmp(chomp(S_), " ", 1) == 0 && NF == 4) {
        x1 = atof(F[0]); y1 = atof(F[1]);
        x2 = atof(F[2]); y2 = atof(F[3]);
      }
    }
  } else {
    exit(MISSING_JWC_TEMP_TXT);
  }
  fclose(f);
  f = fopen("jwc_temp.txt", "w");
  fprintf(f, "pt %.15g %.15g\n", x1, y1);
  fprintf(f, "pt %.15g %.15g\n", x2, y2);
  fclose(f);
  return 0;
}

 

○jw.h

/*  jw.h : JW_CAD 外部変形 例題用ヘッダファイル

    動作確認
    MinGW + gcc / g++ 4.5.0
    Borland C++ 5.5.1 for Win32
    Visual C++ 2005 Express Edition
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define PI 3.1415926535897932384626433832795
#define MISSING_JWC_TEMP_TXT printf("jwc_temp.txt が 見当たりません\n")
#define NO_OPERATION printf("処理できませんでした\n")

int split(char *S_, char **F)
{   /* char S_[256], *F[20]; */
    int i, NF;
    for (i = 0; i < 20; i++) {
        if ( (F[i] = strtok(S_, " ") ) == NULL) { NF = i; break; }
        S_ = NULL;
    }
    return NF;
}

char *substr(char *src, int off, int len, char *buf)
{
    return strncpy(buf, src+off, len);
}

char *chomp(char *str)
{
    int l = strlen(str);
    if( l > 0 && str[l-1] == '\n' )
    {
        str[l-1] = '\0';
    }
    return str;
}

double hypot(double x, double y)
{
    return sqrt(x * x + y * y);
}