Repository navigation
Expand file tree
/
Copy pathvis.cpp
More file actions
100 lines (77 loc) · 2.73 KB
/
Copy pathvis.cpp
File metadata and controls
100 lines (77 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include "vis.h"
void EndGraph(FILE* dotf) {
fprintf(dotf, "}");
}
void StartGraph(FILE* dotf) {
fprintf(dotf, "digraph structs {\n"
"rankdir=TB;\n"
"node[color=\"black\",fontsize=14];\n"
"nodesep = 0.1;\n"
"edge[color=\"darkblue\",fontcolor=\"yellow\",fontsize=12];\n");
}
void MakeImgFromDot(const char* dot_file) {
char img_name[MAX_IMG_FILE_LEN] = "";
snprintf(img_name, MAX_IMG_FILE_LEN, "%simg%lu_%lu.png", IMG_FOLDER_DIR, IMG_CNT++, clock());
char dot_command[MAX_CMD_LEN] = "";
snprintf(dot_command, MAX_CMD_LEN, "dot %s -T png -o %s", dot_file, img_name);
system(dot_command);
printf("<img src=\"%s\"><br>", img_name);
}
void EndGraphic(FILE* gnuf) {
fprintf(gnuf, "set out\n");
}
void StartGraphic(const char* gpl_file, const char* img_name) {
assert(gpl_file);
assert(img_name);
FILE* gnuplot_file = fopen(gpl_file, "a");
if (gnuplot_file != NULL) {
fprintf(gnuplot_file, "#! /opt/homebrew/Cellar/gnuplot -persist\n"
"reset\n"
"set terminal png size 800, 600\n"
"set output '%s'\n"
"set xlabel \"X\"\n"
"set ylabel \"Y\"\n"
"set grid\n", img_name);
fclose(gnuplot_file);
char gnu_command[MAX_CMD_LEN] = "";
snprintf(gnu_command, MAX_CMD_LEN, "gnuplot %s", gpl_file);
//system(gnu_command);
printf("\n<img src=\"%s\"><br>\n", img_name);
} else {
printf("Error: Unable to open Gnuplot script file.\n");
}
}
char* GenImgName() {
char* img_name = (char*)calloc(MAX_IMG_FILE_LEN, sizeof(char));
if (!img_name)
return NULL;
snprintf(img_name, MAX_IMG_FILE_LEN, "%simg%lu_%lu.png", IMG_FOLDER_DIR, IMG_CNT++, clock());
return img_name;
}
void MakeImgFromGpl(const char* gpl_file, const char* img_name) {
assert(gpl_file);
assert(img_name);
char gnu_command[MAX_CMD_LEN] = "";
snprintf(gnu_command, MAX_CMD_LEN, "gnuplot %s", gpl_file);
system(gnu_command);
printf("\n<img src=\"%s\"><br>\n", img_name);
}
void CreatePlotScript(const char* script_file, const char* equation) {
FILE* script = fopen(script_file, "w");
assert(script != NULL);
fprintf(script, "plot %s\n", equation);
fclose(script);
}
int VisCreat(const char* equation) {
const char* gpl_file = "plot.gpl";
CreatePlotScript(gpl_file, equation);
char* img_name = GenImgName();
StartGraphic(gpl_file, img_name);
MakeImgFromGpl(gpl_file, img_name);
free(img_name);
return 0;
}