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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//! 結合の描き方
//!
//! 単結合と二重結合と三重結合は線で引く。二重結合は、鎖の中では真ん中をはさんで両側に、
//! 環の中では内側だけに寄せる。内側に寄せた線は両端を少し詰めて、隣の辺の線と端を合わせる。
//!
//! くさびは三角形で描く。塗るもの、塗らないもの、破線のものがあり、
//! どれも太い側が手前を表す。破線のくさびだけは、短い線を並べて作る。
use crate::node::Kind;
use crate::style::Style;
/// 引かれた 1 本の線
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Line {
pub x1: f64,
pub y1: f64,
pub x2: f64,
pub y2: f64,
}
/// くさびの三角形
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Wedge {
/// 細い側の点
pub tip: (f64, f64),
/// 太い側の 2 点
pub base: [(f64, f64); 2],
/// 中を塗るか
pub filled: bool,
}
/// 二重結合をどちら側に引くか
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Placement {
/// 真ん中をはさんで両側へ引く
Middle,
/// 片側へ寄せる。寄せる先の点と、内側の線を両端で詰める量
Toward {x: f64, y: f64, inset: f64},
}
/// 結合 1 本の描き方
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Drawn {
pub line: Vec<Line>,
pub wedge: Vec<Wedge>,
}
/// 結合 1 本を線と三角形にする
pub fn draw(
kind: Kind,
start: (f64, f64),
end: (f64, f64),
placement: Placement,
style: &Style,
) -> Drawn {
let span = length(start, end);
if span <= 0.0 {
return Drawn::default();
}
// 結合の向きに直交する単位ベクトル
let across = (-(end.1 - start.1) / span, (end.0 - start.0) / span);
let gap = style.double_gap;
match kind {
Kind::Single => line_only(vec![offset(start, end, across, 0.0)]),
Kind::Double => line_only(double(start, end, across, placement, gap)),
Kind::Triple => line_only(vec![
offset(start, end, across, gap),
offset(start, end, across, 0.0),
offset(start, end, across, -gap),
]),
_ => cram(kind, start, end, across, span, style),
}
}
/// 2 点のあいだの長さ
pub fn length(start: (f64, f64), end: (f64, f64)) -> f64 {
((end.0 - start.0).powi(2) + (end.1 - start.1).powi(2)).sqrt()
}
fn line_only(line: Vec<Line>) -> Drawn {
Drawn {line, wedge: Vec::new()}
}
/// 二重結合の 2 本
fn double(
start: (f64, f64),
end: (f64, f64),
across: (f64, f64),
placement: Placement,
gap: f64,
) -> Vec<Line> {
let Placement::Toward {x, y, inset} = placement else {
return vec![
offset(start, end, across, gap / 2.0),
offset(start, end, across, -gap / 2.0),
];
};
// 寄せる先が across のどちら側にあるかを見る
let middle = ((start.0 + end.0) / 2.0, (start.1 + end.1) / 2.0);
let toward = (x - middle.0) * across.0 + (y - middle.1) * across.1;
let side = if toward >= 0.0 {gap} else {-gap};
let inner = offset(start, end, across, side);
vec![offset(start, end, across, 0.0), trim(inner, inset)]
}
/// くさびのたぐい
fn cram(
kind: Kind,
start: (f64, f64),
end: (f64, f64),
across: (f64, f64),
span: f64,
style: &Style,
) -> Drawn {
let (tip, wide) = if kind.is_reversed() {(end, start)} else {(start, end)};
let half = style.wedge_width / 2.0;
if matches!(kind, Kind::Hash | Kind::HashBack) {
return line_only(hash(tip, wide, across, span, style));
}
Drawn {
line: Vec::new(),
wedge: vec![Wedge {
tip,
base: [
(wide.0 + across.0 * half, wide.1 + across.1 * half),
(wide.0 - across.0 * half, wide.1 - across.1 * half),
],
filled: matches!(kind, Kind::Wedge | Kind::WedgeBack),
}],
}
}
/// 破線のくさび。細い側から太い側へ、少しずつ長くなる線を並べる
fn hash(
tip: (f64, f64),
wide: (f64, f64),
across: (f64, f64),
span: f64,
style: &Style,
) -> Vec<Line> {
let count = (span / style.hash_gap).round().max(2.0) as usize;
let half = style.wedge_width / 2.0;
let mut found = Vec::with_capacity(count);
for step in 1..=count {
let ratio = step as f64 / count as f64;
let middle = (
tip.0 + (wide.0 - tip.0) * ratio,
tip.1 + (wide.1 - tip.1) * ratio,
);
let reach = half * ratio;
found.push(Line {
x1: middle.0 + across.0 * reach,
y1: middle.1 + across.1 * reach,
x2: middle.0 - across.0 * reach,
y2: middle.1 - across.1 * reach,
});
}
found
}
/// 両端を across の向きへ amount だけずらした線
fn offset(start: (f64, f64), end: (f64, f64), across: (f64, f64), amount: f64) -> Line {
Line {
x1: start.0 + across.0 * amount,
y1: start.1 + across.1 * amount,
x2: end.0 + across.0 * amount,
y2: end.1 + across.1 * amount,
}
}
/// 線の両端を amount だけ詰める
fn trim(line: Line, amount: f64) -> Line {
let span = length((line.x1, line.y1), (line.x2, line.y2));
if amount <= 0.0 || span <= amount * 2.0 {
return line;
}
let step = ((line.x2 - line.x1) / span * amount, (line.y2 - line.y1) / span * amount);
Line {
x1: line.x1 + step.0,
y1: line.y1 + step.1,
x2: line.x2 - step.0,
y2: line.y2 - step.1,
}
}
#[cfg(test)]
mod tests {
use super::*;
fn plain(kind: Kind, start: (f64, f64), end: (f64, f64)) -> Drawn {
draw(kind, start, end, Placement::Middle, &Style::default())
}
#[test]
fn a_single_bond_is_one_line() {
let found = plain(Kind::Single, (0.0, 0.0), (1.0, 0.0));
assert_eq!(found.line, vec![Line {x1: 0.0, y1: 0.0, x2: 1.0, y2: 0.0}]);
assert!(found.wedge.is_empty());
}
#[test]
fn a_double_bond_straddles_the_line() {
let style = Style::default();
let found = plain(Kind::Double, (0.0, 0.0), (1.0, 0.0)).line;
assert_eq!(found.len(), 2);
assert!((found[0].y1 + found[1].y1).abs() < 1e-12, "真ん中をはさんでいません");
assert!(((found[0].y1 - found[1].y1).abs() - style.double_gap).abs() < 1e-12);
}
#[test]
fn a_triple_bond_keeps_the_middle_line() {
let style = Style::default();
let found = plain(Kind::Triple, (0.0, 0.0), (1.0, 0.0)).line;
assert_eq!(found.len(), 3);
assert_eq!(found[1], Line {x1: 0.0, y1: 0.0, x2: 1.0, y2: 0.0});
assert!((found[0].y1.abs() - style.double_gap).abs() < 1e-12);
assert!((found[0].y1 + found[2].y1).abs() < 1e-12);
}
#[test]
fn a_double_bond_in_a_ring_leans_to_the_inside() {
let style = Style::default();
let toward = Placement::Toward {x: 0.5, y: 1.0, inset: 0.0};
let found = draw(Kind::Double, (0.0, 0.0), (1.0, 0.0), toward, &style).line;
assert_eq!(found.len(), 2);
let edge = Line {x1: 0.0, y1: 0.0, x2: 1.0, y2: 0.0};
assert_eq!(found[0], edge, "外側の線が辺の上にありません");
assert!(found[1].y1 > 0.0, "内側の線が中心の側にありません");
assert!((found[1].y1 - style.double_gap).abs() < 1e-12);
}
#[test]
fn the_inside_line_is_trimmed_at_both_ends() {
let style = Style::default();
let toward = Placement::Toward {x: 0.5, y: 1.0, inset: 0.1};
let found = draw(Kind::Double, (0.0, 0.0), (1.0, 0.0), toward, &style).line;
assert!((found[1].x1 - 0.1).abs() < 1e-12);
assert!((found[1].x2 - 0.9).abs() < 1e-12);
}
#[test]
fn the_inside_line_flips_with_the_center() {
let style = Style::default();
let below = Placement::Toward {x: 0.5, y: -1.0, inset: 0.0};
let found = draw(Kind::Double, (0.0, 0.0), (1.0, 0.0), below, &style).line;
assert!(found[1].y1 < 0.0);
}
#[test]
fn a_wedge_is_a_triangle_with_its_point_at_the_start() {
let style = Style::default();
let found = plain(Kind::Wedge, (0.0, 0.0), (1.0, 0.0)).wedge;
assert_eq!(found.len(), 1);
assert_eq!(found[0].tip, (0.0, 0.0));
assert!(found[0].filled);
assert!((found[0].base[0].1 - found[0].base[1].1).abs() - style.wedge_width < 1e-12);
assert_eq!(found[0].base[0].0, 1.0);
}
#[test]
fn the_backward_wedge_turns_around() {
let found = plain(Kind::WedgeBack, (0.0, 0.0), (1.0, 0.0)).wedge;
assert_eq!(found[0].tip, (1.0, 0.0));
assert_eq!(found[0].base[0].0, 0.0);
}
#[test]
fn a_hollow_wedge_is_not_filled() {
assert!(!plain(Kind::Hollow, (0.0, 0.0), (1.0, 0.0)).wedge[0].filled);
assert!(!plain(Kind::HollowBack, (0.0, 0.0), (1.0, 0.0)).wedge[0].filled);
}
#[test]
fn a_hashed_wedge_is_a_row_of_growing_lines() {
let found = plain(Kind::Hash, (0.0, 0.0), (3.0, 0.0));
assert!(found.wedge.is_empty());
assert!(found.line.len() >= 2);
let first = (found.line[0].y1 - found.line[0].y2).abs();
let last = found.line.last().map(|one| (one.y1 - one.y2).abs()).unwrap();
assert!(first < last, "細い側から太い側へ広がっていません");
assert!((last - Style::default().wedge_width).abs() < 1e-12);
}
#[test]
fn the_hashed_wedge_can_face_the_other_way() {
let found = plain(Kind::HashBack, (0.0, 0.0), (3.0, 0.0)).line;
let first = (found[0].y1 - found[0].y2).abs();
assert!(found[0].x1 > 2.0, "細い側が終わりの側にありません");
assert!(first < Style::default().wedge_width);
}
#[test]
fn the_offset_follows_the_direction_of_the_bond() {
let style = Style::default();
let found = plain(Kind::Double, (0.0, 0.0), (0.0, 1.0)).line;
assert!(((found[0].x1 - found[1].x1).abs() - style.double_gap).abs() < 1e-12);
assert_eq!(found[0].y1, 0.0);
}
#[test]
fn a_bond_of_no_length_draws_nothing() {
let found = plain(Kind::Single, (1.0, 1.0), (1.0, 1.0));
assert!(found.line.is_empty() && found.wedge.is_empty());
}
}