-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Pandicon/main
Add polygons support and prepare for version 0.2.0
- Loading branch information
Showing
13 changed files
with
456 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Examples | ||
This folder contains examples for using the library. You can run each of them with `cargo run --example <example-name>` (for example `cargo run --example cli_polygon_test`). | ||
|
||
## cli polygon test | ||
The core of this example was developed by [@bipentihexium](https://github.com/bipentihexium). It is a cli utility to test points on the sphere if they are inside a polygon. The default polygon is a non-convex one that kept causing issues in development :D It accepts several command line arguments, in order: | ||
- `<height>` - how many rows to print the result into (kind of resolution). If it can not be parsed to an `i32`, defaults to `50`. | ||
- `<width>` - how many columns to print the result into (kind of resolution). If it can not be parsed to an `i32`, defaults to `height*2`. | ||
- `<dec-start>` - the declination to start at, in radians. If it can not be parsed to an `f32`, defaults to `-PI/2.0`. | ||
- `<dec-end>` - the declination to end at, in radians. If it can not be parsed to an `f32`, defaults to `PI/2.0`. | ||
- `<ra-start>` - the right ascension to start at, in radians. If it can not be parsed to an `f32`, defaults to `-PI`. | ||
- `<ra-end>` - the right ascension to end at, in radians. If it can not be parsed to an `f32`, defaults to `PI`. | ||
|
||
Points considered to be inside the polygon are yellow, those outside are purple, and colours in between indicate a state in between - it uses MSAA, so each printed point actually checks multiple points in its area and averages the result. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// The core of this example was developed by [@bipentihexium](https://github.com/bipentihexium) | ||
|
||
use spherical_geometry::{EdgeDirection, Polygon, SphericalPoint}; | ||
use std::f32::consts::PI; | ||
|
||
const DEFAULT_Y_RANGE: i32 = 50; | ||
|
||
fn main() { | ||
let args: Vec<String> = std::env::args().collect(); | ||
|
||
let pol = Polygon::new( | ||
vec![ | ||
SphericalPoint::new(0.0, 0.0), | ||
SphericalPoint::new(0.0, 0.5), | ||
SphericalPoint::new(1.2, 0.5), | ||
SphericalPoint::new(0.8, 0.25), | ||
SphericalPoint::new(1.2, 0.0), | ||
], | ||
EdgeDirection::CounterClockwise, | ||
) | ||
.expect("The polygon should be constructable"); | ||
let y_range = if args.len() < 2 { DEFAULT_Y_RANGE } else { args[1].parse::<i32>().unwrap_or(DEFAULT_Y_RANGE) }; | ||
let x_range = if args.len() < 3 { y_range * 2 } else { args[2].parse::<i32>().unwrap_or(y_range * 2) }; | ||
let (y_start, y_end) = if args.len() < 5 { | ||
(-PI / 2.0, PI / 2.0) | ||
} else { | ||
(args[3].parse::<f32>().unwrap_or(-PI / 2.0), args[4].parse::<f32>().unwrap_or(PI / 2.0)) | ||
}; | ||
let (x_start, x_end) = if args.len() < 7 { | ||
(-PI, PI) | ||
} else { | ||
(args[5].parse::<f32>().unwrap_or(-PI), args[6].parse::<f32>().unwrap_or(PI)) | ||
}; | ||
let ysr = 4; | ||
let xsr = 2; | ||
for y in 0..y_range { | ||
for x in 0..x_range { | ||
let xfns = (x as f32) / (x_range as f32); | ||
let yfns = (y as f32) / (y_range as f32); | ||
let pns = SphericalPoint::new((x_end - x_start) * xfns + x_start, -((y_end - y_start) * yfns + y_start)); | ||
let mut inc = 0u32; | ||
let samples = xsr * ysr; | ||
for sy in 0..ysr { | ||
for sx in 0..xsr { | ||
let xf = ((x as f32) + (sx as f32) / (xsr as f32)) / (x_range as f32); | ||
let yf = ((y as f32) + (sy as f32) / (ysr as f32)) / (y_range as f32); | ||
let p = SphericalPoint::new((x_end - x_start) * xf + x_start, -((y_end - y_start) * yf + y_start)); | ||
if pol.contains_point(&p).unwrap() { | ||
inc += 1; | ||
} | ||
} | ||
} | ||
let col = (inc as f32) / (samples as f32); | ||
let col_r = 255.0 * (2.83 * col * col - 2.36 * col + 0.5); | ||
let col_g = 255.0 * (2.0 * col - col * col); | ||
let col_b = 255.0 * 0.7 * (1.0 - col * col); | ||
let isp = pol.vertices().iter().map(|p2| pns.distance(p2)).fold(f32::INFINITY, |a, b| a.min(b)) < 0.1; | ||
print!("\x1b[48;2;{};{};{}m{}", col_r as u32, col_g as u32, col_b as u32, if isp { '#' } else { ' ' }); | ||
} | ||
println!("\x1b[0m"); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.