FuncSketch
Loading...
Searching...
No Matches
plot_range.h
Go to the documentation of this file.
1/*
2 * Copyright 2026 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <cmath>
23#include <utility>
24
25#include <fmt/base.h>
26
29
30namespace func_sketch::plotter {
31
35class PlotRange {
36public:
43 PlotRange(const std::pair<double, double>& x_range,
44 const std::pair<double, double>& y_range)
46 if (!std::isfinite(x_range.first) || !std::isfinite(x_range.second)) {
47 throw InvalidArgumentException("Invalid X range: not finite");
48 }
49 if (!std::isfinite(y_range.first) || !std::isfinite(y_range.second)) {
50 throw InvalidArgumentException("Invalid Y range: not finite");
51 }
52 if (x_range.first >= x_range.second) {
53 throw InvalidArgumentException("Invalid X range: min >= max");
54 }
55 if (y_range.first >= y_range.second) {
56 throw InvalidArgumentException("Invalid Y range: min >= max");
57 }
58 }
59
65 [[nodiscard]] auto x_range() const noexcept -> std::pair<double, double> {
66 return x_range_;
67 }
68
74 [[nodiscard]] auto y_range() const noexcept -> std::pair<double, double> {
75 return y_range_;
76 }
77
85 [[nodiscard]] bool contains(const Point& point) const noexcept {
86 return std::isfinite(point.x) && std::isfinite(point.y) &&
87 x_range_.first <= point.x && point.x <= x_range_.second &&
88 y_range_.first <= point.y && point.y <= y_range_.second;
89 }
90
91private:
93 std::pair<double, double> x_range_;
94
96 std::pair<double, double> y_range_;
97};
98
99} // namespace func_sketch::plotter
100
105template <>
106struct fmt::formatter<func_sketch::plotter::PlotRange>
107 : fmt::formatter<string_view> {
115 auto format(const func_sketch::plotter::PlotRange& value,
116 format_context& context) const -> format_context::iterator;
117};
Class of a range of a plot.
Definition plot_range.h:35
std::pair< double, double > x_range_
X range.
Definition plot_range.h:93
PlotRange(const std::pair< double, double > &x_range, const std::pair< double, double > &y_range)
Constructor.
Definition plot_range.h:43
bool contains(const Point &point) const noexcept
Check if a point is in the range.
Definition plot_range.h:85
auto y_range() const noexcept -> std::pair< double, double >
Get Y range.
Definition plot_range.h:74
std::pair< double, double > y_range_
Y range.
Definition plot_range.h:96
auto x_range() const noexcept -> std::pair< double, double >
Get X range.
Definition plot_range.h:65
Definition of exception classes.
Definition of Point struct.
auto format(const func_sketch::plotter::PlotRange &value, format_context &context) const -> format_context::iterator
Format a value.
Struct of a point.
Definition point.h:29