Crates.io | query-curve |
lib.rs | query-curve |
version | 0.1.0 |
source | src |
created_at | 2024-10-21 05:07:01.448165 |
updated_at | 2024-10-21 05:07:01.448165 |
description | A Rust library for querying custom Bezier curves, compatible with curves created at https://querycurve.com. |
homepage | https://querycurve.com/ |
repository | https://github.com/ralusek/query-curve |
max_upload_size | |
id | 1416930 |
size | 23,306 |
This tool allows you to invoke queries against a curve you've laid out at https://querycurve.com
Once you have a curve in the shape you'd like:
You'll get a resulting encoded curve that'll look like this:
2BLnMW-2BLnMW--KyjA--KyjA-0-KyjA-CaR6-XZAG-KyjA-TN1E-KyjA-KyjA-KyjA-CaR6-TN1E-8OI4-fxSK-KyjA
Add query-curve to your Cargo.toml dependencies:
[dependencies]
query-curve = "0.1.0"
Then run:
cargo build
use query_curve::query_encoded_curve;
fn main() {
let curve = "5SNUPI-8nlt2n2-0-0-0-fxSK-3yGp-fn3A-TzAp-e6zY-bau8-PAsC-dGxk-LXPh-f3xT-9cbF-fxSK-0";
let x_value = 0.0;
let result = query_encoded_curve(&curve.to_string(), x_value);
match result {
Some(y) => println!("At x = {}, y = {}", x_value, y),
None => println!("Failed to find y for x = {}", x_value),
}
}
If you are pulling your curve from a db or otherwise need it to be dynamic:
use query_curve::query_encoded_curve;
fn main() {
// Assume this was loaded from a database
let dynamically_loaded_curve = "fxSK-fxSK-0-0-0-0-KyjA-0-KyjA-fxSK-fxSK-fxSK";
let my_x_value = 0.35;
// Gets the corresponding y value along the curve for a given x
let result = query_encoded_curve(&dynamically_loaded_curve.to_string(), my_x_value);
match result {
Some(y) => println!("At x = {}, y = {}", my_x_value, y),
None => println!("Failed to find y for x = {}", my_x_value),
}
}
Note: While decoding the curve is fast, repeatedly querying against the same curve can be optimized by preloading the curve. If you anticipate multiple queries against the same curve, consider using:
If the curve you're using will be used to facilitate multiple queries, this alternative for querying will bypass the need to decode the curve on every query.
use query_curve::get_encoded_curve_query_function;
fn main() {
let fixed_curve = "fxSK-fxSK-0-0-0-0-KyjA-0-KyjA-fxSK-fxSK-fxSK";
// Returns a function with a reference to the decoded curve
let query_my_curve = get_encoded_curve_query_function(&fixed_curve.to_string()).unwrap();
let x_values = [0.0, 0.5, 0.37];
for &x in &x_values {
match query_my_curve(x) {
Some(y) => println!("At x = {}, y = {}", x, y),
None => println!("Failed to find y for x = {}", x),
}
}
}
Efficient Querying: Quickly find the y value for any given x on your custom Bezier curve.
Preloading Curves: Optimize performance by preloading and reusing decoded curves for multiple queries.
Integration with QueryCurve.com: Seamlessly use curves designed with QueryCurve.com.
Full documentation is available at docs.rs/query-curve.
Contributions are welcome! Please feel free to submit a pull request or open an issue.
This project is licensed under the MIT License - see the LICENSE file for details.