pub struct Point {
pub x: f64,
pub y: f64,
}Expand description
A 2D point.
This type represents a point in 2D space. It has the same layout as Vec2, but
its meaning is different: Vec2 represents a change in location (for example velocity).
In general, kurbo overloads math operators where it makes sense, for example implementing
Affine * Point as the point under the affine transformation. However Point + Point and
f64 * Point are not implemented, because the operations do not make geometric sense. If you
need to apply these operations, then 1) check what you’re doing makes geometric sense, then 2)
use Point::to_vec2 to convert the point to a Vec2.
Fields§
§x: f64The x coordinate.
y: f64The y coordinate.
Implementations§
Source§impl Point
impl Point
Sourcepub const fn new(x: f64, y: f64) -> Point
pub const fn new(x: f64, y: f64) -> Point
Create a new Point with the provided x and y coordinates.
Examples found in repository?
10fn main() {
11 let mut scene = Scene::new(400.0, 300.0).with_background(Color::WHITE);
12
13 let style = TextStyle::new(Color::BLACK, 20.0, "sans-serif")
14 .with_align(TextAlign::Left)
15 .with_baseline(TextBaseline::Top)
16 .with_rotation(45_f64.to_radians());
17
18 scene.push(Element::text(
19 "2026-10-01",
20 Point::new(50.0, 80.0),
21 style.clone(),
22 ));
23 scene.push(Element::text(
24 "不旋转的对照",
25 Point::new(50.0, 220.0),
26 style.with_rotation(0.0),
27 ));
28
29 // SVG
30 let mut r = SvgRenderer::new(400.0, 300.0);
31 r.render_scene(&scene);
32 std::fs::write("rot_text.svg", r.into_string()).unwrap();
33 println!("wrote rot_text.svg");
34
35 // PNG
36 let mut r = VelloPixmapRenderer::new(400, 300);
37 let pixmap = r.render_scene_to_pixmap(&scene);
38 std::fs::write("rot_text.png", pixmap.to_png()).unwrap();
39 println!("wrote rot_text.png");
40}More examples
55fn build_scene() -> Scene {
56 let mut scene = Scene::new(W, H);
57
58 // 网格(独立图层,用来演示"网格不参与手绘")。
59 let mut grid = Layer::new("grid");
60 for i in 0..=7 {
61 let x = i as f64 * 80.0;
62 grid.push(Element::line(
63 Point::new(x, 0.0),
64 Point::new(x, H),
65 Stroke::new(grid_color(), 1.0),
66 ));
67 }
68 for j in 0..=3 {
69 let y = j as f64 * 80.0;
70 grid.push(Element::line(
71 Point::new(0.0, y),
72 Point::new(W, y),
73 Stroke::new(grid_color(), 1.0),
74 ));
75 }
76 scene.push_layer(grid);
77
78 // 两个圆角矩形节点 + 连线。
79 scene.push(Element::rounded_rect(
80 Rect::new(40.0, 40.0, 190.0, 100.0),
81 10.0,
82 node_style(node_fill()),
83 ));
84 scene.push(label("开始", Point::new(115.0, 76.0)));
85 scene.push(Element::rounded_rect(
86 Rect::new(330.0, 40.0, 480.0, 100.0),
87 10.0,
88 node_style(node_fill()),
89 ));
90 scene.push(label("结束", Point::new(405.0, 76.0)));
91 scene.push(Element::poly(
92 vec![
93 Point::new(190.0, 70.0),
94 Point::new(260.0, 70.0),
95 Point::new(260.0, 160.0),
96 Point::new(330.0, 160.0),
97 ],
98 Stroke::new(ink(), 2.0),
99 ));
100
101 // 一排几何图元,覆盖 circle / ellipse / polygon / pie。
102 scene.push(Element::circle(
103 Point::new(80.0, 180.0),
104 26.0,
105 node_style(Color::rgb(0xFF, 0xE0, 0xE0)),
106 ));
107 scene.push(Element::ellipse(
108 Point::new(200.0, 180.0),
109 Vec2::new(44.0, 22.0),
110 0.0,
111 node_style(Color::rgb(0xE0, 0xFF, 0xE0)),
112 ));
113 scene.push(Element::polygon(
114 vec![
115 Point::new(320.0, 150.0),
116 Point::new(360.0, 180.0),
117 Point::new(320.0, 210.0),
118 Point::new(280.0, 180.0),
119 ],
120 node_style(Color::rgb(0xFF, 0xF3, 0xD6)),
121 ));
122 scene.push(Element::pie(
123 Point::new(460.0, 180.0),
124 34.0,
125 0.35,
126 4.6,
127 node_style(Color::rgb(0xEF, 0xE0, 0xFF)),
128 ));
129
130 scene
131}21fn build_scene() -> Scene {
22 let mut scene = Scene::new(560.0, 400.0)
23 .with_background(Color::WHITE)
24 .with_title("lievisual 丰富 IR 演示")
25 .with_description("展示图层 + 椭圆/圆角矩形/多边形/圆弧/扇形、径向渐变、虚线、透明度与命名")
26 .with_scale(2.0);
27
28 // ---------- 默认层(nodes):网格虚线 ----------
29 let dashed = Stroke::dashed(Color::rgb(0xbb, 0xbb, 0xbb), 1.0, vec![6.0, 4.0]);
30 scene.push(Element::poly(
31 vec![
32 Point::new(20.0, 260.0),
33 Point::new(120.0, 220.0),
34 Point::new(220.0, 260.0),
35 Point::new(320.0, 220.0),
36 ],
37 dashed,
38 ));
39 scene.push(Element::line(
40 Point::new(20.0, 300.0),
41 Point::new(540.0, 300.0),
42 Stroke::new(Color::rgb(0xdd, 0xdd, 0xdd), 1.0),
43 ));
44
45 // ---------- shapes 层 ----------
46 let linear = LinearGradient::new(
47 Point::new(0.0, 0.0),
48 Point::new(200.0, 200.0),
49 vec![
50 GradientStop {
51 offset: 0.0,
52 color: Color::rgb(0x00, 0x66, 0xcc),
53 },
54 GradientStop {
55 offset: 1.0,
56 color: Color::rgb(0xcc, 0x00, 0x66),
57 },
58 ],
59 );
60 let mut shapes = Layer::new("shapes").with_opacity(0.95);
61
62 // 圆(描边)
63 shapes.push(
64 SceneNode::new(Element::circle(
65 Point::new(100.0, 100.0),
66 60.0,
67 FillStrokeStyle::stroke(Color::BLACK, 2.0),
68 ))
69 .with_name("circle-outline"),
70 );
71 // 椭圆(线性渐变填充 + 旋转)
72 shapes.push(
73 SceneNode::new(Element::ellipse(
74 Point::new(180.0, 80.0),
75 Vec2::new(55.0, 28.0),
76 0.5,
77 FillStrokeStyle {
78 fill: Some(linear.clone().into()),
79 stroke: Some(Stroke::new(Color::rgb(0x22, 0x22, 0x22), 1.5)),
80 },
81 ))
82 .with_name("ellipse-rotated"),
83 );
84 // 圆角矩形(径向渐变填充)
85 let radial = RadialGradient::new(
86 Point::new(300.0, 70.0),
87 60.0,
88 vec![
89 GradientStop {
90 offset: 0.0,
91 color: Color::rgb(0xff, 0xff, 0xff),
92 },
93 GradientStop {
94 offset: 1.0,
95 color: Color::rgb(0xee, 0x99, 0x00),
96 },
97 ],
98 );
99 shapes.push(
100 SceneNode::new(Element::rounded_rect(
101 lievisual::Rect::new(270.0, 30.0, 120.0, 70.0),
102 14.0,
103 FillStrokeStyle {
104 fill: Some(radial.into()),
105 stroke: Some(Stroke::new(Color::rgb(0x33, 0x33, 0x33), 1.0)),
106 },
107 ))
108 .with_name("rounded-card"),
109 );
110 // 多边形
111 shapes.push(SceneNode::new(Element::polygon(
112 vec![
113 Point::new(400.0, 150.0),
114 Point::new(450.0, 90.0),
115 Point::new(520.0, 120.0),
116 Point::new(500.0, 200.0),
117 Point::new(420.0, 200.0),
118 ],
119 FillStrokeStyle {
120 fill: Some(Color::rgb(0x66, 0xcc, 0x66).into()),
121 stroke: Some(Stroke::new(Color::rgb(0x22, 0x66, 0x22), 1.5)),
122 },
123 )));
124 // 圆弧
125 shapes.push(SceneNode::new(Element::arc(
126 Point::new(120.0, 320.0),
127 Vec2::new(50.0, 40.0),
128 0.3,
129 4.2,
130 Stroke::new(Color::rgb(0x00, 0x88, 0x88), 3.0),
131 )));
132 // 裁剪:把超出圆形的矩形部分裁掉(clip 演示)
133 shapes.push(
134 SceneNode::new(Element::rect(
135 lievisual::Rect::new(20.0, 20.0, 80.0, 80.0),
136 FillStrokeStyle::fill(Color::rgb(0x88, 0x44, 0xcc)),
137 ))
138 .with_transform(Transform::translate(360.0, 60.0))
139 .with_clip(lievisual::Clip::circle(Point::new(40.0, 40.0), 30.0)),
140 );
141 // 组合 + 变换 + 透明度
142 let box_children = vec![
143 SceneNode::new(Element::rounded_rect(
144 lievisual::Rect::new(0.0, 0.0, 90.0, 50.0),
145 8.0,
146 FillStrokeStyle::fill(Color::rgb(0xff, 0xcc, 0x00)),
147 )),
148 SceneNode::new(Element::text(
149 "group",
150 Point::new(12.0, 30.0),
151 TextStyle::new(Color::rgb(0x33, 0x33, 0x33), 14.0, "sans-serif"),
152 )),
153 ];
154 shapes.push(
155 SceneNode::group(box_children)
156 .with_transform(Transform::translate(430.0, 260.0).then(&Transform::rotate(0.25)))
157 .with_name("demo-group"),
158 );
159
160 // ---------- pie 层(饼图 + 层内节点透明度分级) ----------
161 let pie_style = |c: Color| FillStrokeStyle {
162 fill: Some(c.into()),
163 stroke: Some(Stroke::new(Color::WHITE, 1.5)),
164 };
165 let mut pie = Layer::new("pie");
166 pie.push_node(SceneNode::new(Element::pie(
167 Point::new(300.0, 320.0),
168 70.0,
169 0.0,
170 1.2,
171 pie_style(Color::rgb(0xe6, 0x19, 0x4b)),
172 )));
173 pie.push_node(
174 SceneNode::new(Element::pie(
175 Point::new(300.0, 320.0),
176 70.0,
177 1.2,
178 1.6,
179 pie_style(Color::rgb(0xf0, 0x82, 0x0c)),
180 ))
181 .with_opacity(0.75),
182 );
183 pie.push_node(
184 SceneNode::new(Element::pie(
185 Point::new(300.0, 320.0),
186 70.0,
187 2.8,
188 1.4,
189 pie_style(Color::rgb(0x9a, 0xC7, 0x00)),
190 ))
191 .with_opacity(0.5),
192 );
193 pie.push_node(
194 SceneNode::new(Element::pie(
195 Point::new(300.0, 320.0),
196 70.0,
197 4.2,
198 2.08,
199 pie_style(Color::rgb(0x42, 0x62, 0xac)),
200 ))
201 .with_opacity(0.25),
202 );
203
204 // ---------- labels 层(顶部文本;演示 baseline/weight/align 与 measure_text) ----------
205 let title_style = TextStyle::new(Color::rgb(0x11, 0x11, 0x11), 20.0, "sans-serif")
206 .with_weight(lievisual::FontWeight::Bold)
207 .with_align(lievisual::TextAlign::Left);
208 // 先测量再做水平居中(canvas 风格:measureText → 用 width 定位锚点)。
209 // 文本以样式化片段表达:单个 RichSpan 即单文本。
210 let m = lievisual::measure_text(
211 std::slice::from_ref(&lievisual::RichSpan::new(
212 "lievisual · 丰富 IR · Layer",
213 title_style.clone(),
214 )),
215 None,
216 );
217 let title_x = 280.0 - m.size.width / 2.0; // 画布中心 280 居中
218 let labels = Layer::new("labels").with_nodes(vec![SceneNode::new(Element::text(
219 "lievisual · 丰富 IR · Layer",
220 Point::new(title_x, 380.0),
221 title_style,
222 ))]);
223
224 // ---------- canvas-api 层:Canvas-like builder(Ctx + Path2D) ----------
225 // 与上面手写 IR 的图层等价,但用 canvas 风格表达:不可变 Ctx 携带 fill / stroke /
226 // transform / alpha,Path2D 累积路径后由 fill() / stroke() 终结。
227 let mut canvas_api = Layer::new("canvas-api");
228 let ctx = Ctx::new()
229 .with_fill(Color::rgb(0x2e, 0x8b, 0x57))
230 .with_line(Color::rgb(0x1f, 0x5f, 0x3d), 2.0)
231 .with_alpha(0.85);
232
233 // 叶片轮廓:三次贝塞尔 + close_path,填充并描边。
234 let mut leaf = Path2D::new();
235 leaf.move_to(0.0, 0.0);
236 leaf.curve_to(20.0, -30.0, 50.0, -30.0, 60.0, 0.0);
237 leaf.curve_to(50.0, 30.0, 20.0, 30.0, 0.0, 0.0);
238 leaf.close_path();
239 canvas_api.push(ctx.clone().translate(410.0, 330.0).fill_stroke_path(&leaf));
240
241 // 四分之三圆环:Path2D::arc(内部用 ≤90° 的三次贝塞尔近似),仅描边。
242 let mut ring = Path2D::new();
243 ring.arc(0.0, 0.0, 18.0, 0.0, std::f64::consts::TAU * 0.75);
244 canvas_api.push(
245 ctx.clone()
246 .with_line(Color::rgb(0xc0, 0x39, 0x2b), 3.0)
247 .with_dash(vec![5.0, 3.0], 0.0)
248 .translate(500.0, 330.0)
249 .stroke(&ring),
250 );
251
252 // 富文本:同一段文本内混排多种样式(Canvas 的 fillText 做不到)。
253 let rich = vec![
254 lievisual::RichSpan::new(
255 "Canvas-like ",
256 TextStyle::new(Color::rgb(0x2c, 0x3e, 0x50), 14.0, "sans-serif")
257 .with_weight(lievisual::FontWeight::Bold),
258 ),
259 lievisual::RichSpan::new(
260 "builder · Path2D",
261 TextStyle::new(Color::rgb(0x7f, 0x8c, 0x8d), 14.0, "sans-serif"),
262 ),
263 ];
264 let m = ctx.measure_rich_text(&rich);
265 canvas_api.push(ctx.fill_rich_text(410.0 - m.size.width / 2.0 + 30.0, 375.0, rich));
266
267 // 图层顺序(从底到顶):默认层 nodes → shapes → pie → canvas-api → labels
268 scene.push_layer(shapes);
269 scene.push_layer(pie);
270 scene.push_layer(canvas_api);
271 scene.push_layer(labels);
272
273 scene
274}Sourcepub fn distance(self, other: Point) -> f64
pub fn distance(self, other: Point) -> f64
Euclidean distance.
See Vec2::hypot for the same operation on Vec2.
Sourcepub fn distance_squared(self, other: Point) -> f64
pub fn distance_squared(self, other: Point) -> f64
Squared Euclidean distance.
See Vec2::hypot2 for the same operation on Vec2.
Sourcepub fn ceil(self) -> Point
pub fn ceil(self) -> Point
Returns a new Point,
with x and y rounded up to the nearest integer,
unless they are already an integer.
§Examples
use kurbo::Point;
let a = Point::new(3.3, 3.6).ceil();
let b = Point::new(3.0, -3.1).ceil();
assert_eq!(a.x, 4.0);
assert_eq!(a.y, 4.0);
assert_eq!(b.x, 3.0);
assert_eq!(b.y, -3.0);Sourcepub fn floor(self) -> Point
pub fn floor(self) -> Point
Returns a new Point,
with x and y rounded down to the nearest integer,
unless they are already an integer.
§Examples
use kurbo::Point;
let a = Point::new(3.3, 3.6).floor();
let b = Point::new(3.0, -3.1).floor();
assert_eq!(a.x, 3.0);
assert_eq!(a.y, 3.0);
assert_eq!(b.x, 3.0);
assert_eq!(b.y, -4.0);Sourcepub fn expand(self) -> Point
pub fn expand(self) -> Point
Returns a new Point,
with x and y rounded away from zero to the nearest integer,
unless they are already an integer.
§Examples
use kurbo::Point;
let a = Point::new(3.3, 3.6).expand();
let b = Point::new(3.0, -3.1).expand();
assert_eq!(a.x, 4.0);
assert_eq!(a.y, 4.0);
assert_eq!(b.x, 3.0);
assert_eq!(b.y, -4.0);Sourcepub fn trunc(self) -> Point
pub fn trunc(self) -> Point
Returns a new Point,
with x and y rounded towards zero to the nearest integer,
unless they are already an integer.
§Examples
use kurbo::Point;
let a = Point::new(3.3, 3.6).trunc();
let b = Point::new(3.0, -3.1).trunc();
assert_eq!(a.x, 3.0);
assert_eq!(a.y, 3.0);
assert_eq!(b.x, 3.0);
assert_eq!(b.y, -3.0);Sourcepub const fn get_coord_mut(&mut self, axis: Axis) -> &mut f64
pub const fn get_coord_mut(&mut self, axis: Axis) -> &mut f64
Get a mutable reference to the member matching the given axis.
Trait Implementations§
Source§impl AddAssign<Vec2> for Point
impl AddAssign<Vec2> for Point
Source§fn add_assign(&mut self, other: Vec2)
fn add_assign(&mut self, other: Vec2)
+= operation. Read more