Текущий технический подход (детальная реализация)
Моя архитектура состоит из двух основных компонентов, которые работают вместе для рендеринга логических элементов из определений JSON:
0. Система определения формы
Каждый компонент определяется в файле JSON (
Код: Выделить всё
canvas_elements.jsonКод: Выделить всё
{
"id": "OR_Gate",
"name": "OR Gate",
"anchorPoint": [80, 60],
"shapes": [
{
"type": "BezierShape",
"p0": {"x": 43, "y": 20},
"p1": {"x": 85, "y": 24},
"p2": {"x": 110, "y": 60},
"color": "#333333"
},
{
"type": "BezierShape",
"p0": {"x": 43, "y": 100},
"p1": {"x": 85, "y": 96},
"p2": {"x": 110, "y": 60},
"color": "#333333"
},
{
"type": "BezierShape",
"p0": {"x": 43, "y": 20},
"p1": {"x": 60, "y": 60},
"p2": {"x": 43, "y": 100},
"color": "#333333"
}
]
}
1. CanvasModel: анализ JSON и фабрика элементов (
Код: Выделить всё
CanvasModel.hКод: Выделить всё
// CanvasModel.h - JSON deserialization entry point
#pragma once
#include
#include
#include
class CanvasElement;
// Global function: JSON -> CanvasElement list
std::vector LoadCanvasElements(const wxString& jsonPath);
2. CanvasElement: хранилище фигур и механизм рендеринга Безье (
Код: Выделить всё
CanvasElement.hКод: Выделить всё
// CanvasElement.h - Core shape storage and Bezier math
#pragma once
#include
#include
#include
#include
#include
struct Point { int x, y; };
// Quadratic Bezier curve definition (loaded directly from JSON)
struct BezierShape {
Point p0, p1, p2;
wxColour color;
BezierShape(Point p0 = Point(), Point p1 = Point(), Point p2 = Point(),
wxColour c = wxColour(0, 0, 0))
: p0(p0), p1(p1), p2(p2), color(c) {}
};
// Shape variant that stores all drawable primitives
using Shape = std::variant[*];
class CanvasElement {
private:
std::vector m_shapes; // All shapes for this element, including Bezier curves
// Core Bezier math: samples quadratic curve into polyline
std::vector CalculateBezier(const Point& p0, const Point& p1,
const Point& p2, int segments = 16) const {
std::vector points;
for (int i = 0; i
Подробнее здесь: [url]https://stackoverflow.com/questions/79849486/how-to-draw-proper-logic-gate-curves-or-nor-shapes-using-quadratic-bezier-curv[/url]
Мобильная версия