Skip to content

Commit

Permalink
version 0.2.6 finished✅
Browse files Browse the repository at this point in the history
1. penguin will create directory `graph` automatically now
2. you can draw many picture in one program now(call `show;` more than one times)
  • Loading branch information
MrZLeo committed Dec 6, 2021
1 parent f12758f commit dba384e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
12 changes: 12 additions & 0 deletions code.pg
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,37 @@ scale is (1, 1);
// draw the graph
set x (-100, 100);
set y (-100, 100);

set size 2;
set color red;
rot is PI / 4;
origin is (-20, -20);
for t from -100 to 100 step 0.8 draw(80 * sin(t), 35 * cos(t)); // circle

set size 1;
set color blue;
rot is 0;
origin is (0, 0);
for t from -100 to 100 step 0.01 draw(t, 40 * sin(t / 2)); //sine

set color green;
for t from -100 to 100 step 0.01 draw(t, 10 * cos(t / 10+ 10)); // cosine

show;

set size 3;
set color cyan;
for t from -100 to 100 step 5 draw(t, t);

set color yellow;
for t from -2*PI to 2*PI step 0.07 draw(60*sin(t), 60*cos(t));

show;

set color MAGENTA;
for t from 0 to 2*PI step 0.1 draw (t, sin(PI/2));

set color black;
for t from 0 to 2*PI step 0.1 draw (t+10, sin(90));

show;
Binary file removed graph/0.png
Binary file not shown.
14 changes: 11 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod rt_util;
mod tree_node;

use std::fs;
use std::io::{self, BufRead, Write};
use std::io::{self, BufRead, ErrorKind, Write};
use lazy_static::lazy_static;
use regex::Regex;
use lrlex::lrlex_mod;
Expand All @@ -18,7 +18,7 @@ lrlex_mod!("lexer.l");
// with a suffix of `_y`).
lrpar_mod!("parser.y");

const VERSION: &str = "0.2.5";
const VERSION: &str = "0.2.6";

lazy_static!(
static ref EXIT: Vec<String> = {
Expand All @@ -44,11 +44,19 @@ fn info() {

fn main() {
info();

#[cfg(feature = "debug")] {
println!("### debug mode is open. ###");
}

// create directory for graph that user will draw
match fs::create_dir("graph") {
Err(e) => match e.kind() {
ErrorKind::AlreadyExists => {}
_ => eprintln!("!{:?}", e),
}
Ok(_) => { println!("# Created directory `graph` for output.") }
}

let runtime = RunTime::new();

let args = std::env::args();
Expand Down
15 changes: 12 additions & 3 deletions src/rt_util.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use std::fmt::format;
use std::process::exit;
use lazy_static::lazy_static;
use lrlex::{DefaultLexeme, lrlex_mod};
use lrpar::lrpar_mod;
use plotters::prelude::*;
use crate::tree_node::TreeNode;
use crate::tree_node;
use crate::{file, tree_node};
lrlex_mod!("lexer.l");
lrpar_mod!("parser.y");


lazy_static!(
static ref LEXER_DEF: lrlex::LRNonStreamingLexerDef<DefaultLexeme, u32> = lexer_l::lexerdef();
static ref PIC_NUM: u32 = 0;
);

#[derive(Debug)]
Expand Down Expand Up @@ -44,6 +47,7 @@ pub struct RunTime {
y_range: (f64, f64),
size: f64,
color: String,
pic_num: u32,
}

impl RunTime {
Expand All @@ -57,6 +61,7 @@ impl RunTime {
y_range: (-4.0, 4.0),
size: 2.0,
color: "blue".to_string(),
pic_num: 0,
}
}

Expand All @@ -71,6 +76,7 @@ impl RunTime {
y_range: (0.0, 0.0),
size,
color,
pic_num: 0,
}
}

Expand Down Expand Up @@ -114,9 +120,11 @@ impl RunTime {
}

pub fn show(&mut self) {
let file_name = format!("graph/{}.png", self.pic_num);
let root =
BitMapBackend::new("graph/0.png", (1024, 1024))
BitMapBackend::new(file_name.as_str(), (1024, 1024))
.into_drawing_area();

root.fill(&WHITE).unwrap();
let mut chart = ChartBuilder::on(&root)
.margin(60)
Expand Down Expand Up @@ -163,8 +171,9 @@ impl RunTime {
},
)).unwrap();
});
println!("Draw success in ./graph/0.png");
println!("Draw success in {}", file_name);
self.graph.clear();
self.pic_num += 1;
}


Expand Down

0 comments on commit dba384e

Please sign in to comment.