[python] matplotlib: improve arrow method, draw arrow edge

This commit is contained in:
Émilie Feral
2020-03-18 18:02:20 +01:00
parent 9e822e85e5
commit b5d3070ef5
4 changed files with 15 additions and 7 deletions

View File

@@ -51,7 +51,7 @@ mp_obj_t modpyplot_arrow(size_t n_args, const mp_obj_t *args) {
assert(sPlotStore != nullptr);
KDColor color = Palette::DataColor[paletteIndex++]; // FIXME: Share overflow routine
sPlotStore->addSegment(args[0], args[1], mp_obj_new_float(mp_obj_get_float(args[0])+mp_obj_get_float(args[2])), mp_obj_new_float(mp_obj_get_float(args[1])+mp_obj_get_float(args[3])), color);
sPlotStore->addSegment(args[0], args[1], mp_obj_new_float(mp_obj_get_float(args[0])+mp_obj_get_float(args[2])), mp_obj_new_float(mp_obj_get_float(args[1])+mp_obj_get_float(args[3])), color, true);
return mp_const_none;
}
@@ -220,7 +220,7 @@ mp_obj_t modpyplot_plot(mp_obj_t x, mp_obj_t y) {
KDColor color = Palette::DataColor[paletteIndex++]; // FIXME: Share overflow routine
for (size_t i=0; i<length-1; i++) {
sPlotStore->addSegment(xItems[i], yItems[i], xItems[i+1], yItems[i+1], color);
sPlotStore->addSegment(xItems[i], yItems[i], xItems[i+1], yItems[i+1], color, false);
}
return mp_const_none;

View File

@@ -78,18 +78,19 @@ template class PlotStore::ListIterator<PlotStore::Segment>;
PlotStore::Segment::Segment(mp_obj_t tuple) {
mp_obj_t * elements;
mp_obj_get_array_fixed_n(tuple, 5, &elements);
mp_obj_get_array_fixed_n(tuple, 6 , &elements);
m_xStart = mp_obj_get_float(elements[0]);
m_yStart = mp_obj_get_float(elements[1]);
m_xEnd = mp_obj_get_float(elements[2]);
m_yEnd = mp_obj_get_float(elements[3]);
m_color = KDColor::RGB16(mp_obj_get_int(elements[4]));
m_arrow = elements[5] == mp_const_true;
}
void PlotStore::addSegment(mp_obj_t xStart, mp_obj_t yStart, mp_obj_t xEnd, mp_obj_t yEnd, KDColor c) {
void PlotStore::addSegment(mp_obj_t xStart, mp_obj_t yStart, mp_obj_t xEnd, mp_obj_t yEnd, KDColor c, bool arrowEdge) {
mp_obj_t color = mp_obj_new_int(c);
mp_obj_t items[5] = {xStart, yStart, xEnd, yEnd, color};
mp_obj_t tuple = mp_obj_new_tuple(5, items);
mp_obj_t items[6] = {xStart, yStart, xEnd, yEnd, color, arrowEdge ? mp_const_true : mp_const_false};
mp_obj_t tuple = mp_obj_new_tuple(6, items);
mp_obj_list_append(m_segments, tuple);
}

View File

@@ -67,16 +67,18 @@ public:
float yStart() const { return m_yStart; }
float xEnd() const { return m_xEnd; }
float yEnd() const { return m_yEnd; }
bool isArrow() const { return m_arrow; }
KDColor color() const { return m_color; }
private:
float m_xStart;
float m_yStart;
float m_xEnd;
float m_yEnd;
bool m_arrow;
KDColor m_color;
};
void addSegment(mp_obj_t xStart, mp_obj_t yStart, mp_obj_t xEnd, mp_obj_t yEnd, KDColor c);
void addSegment(mp_obj_t xStart, mp_obj_t yStart, mp_obj_t xEnd, mp_obj_t yEnd, KDColor c, bool arrowEdge);
Iterable<ListIterator<Segment>> segments() { return Iterable<ListIterator<Segment>>(m_segments); }
// Rect

View File

@@ -43,6 +43,11 @@ void PlotView::traceSegment(KDContext * ctx, KDRect r, PlotStore::Segment segmen
segment.xEnd(), segment.yEnd(),
segment.color()
);
if (segment.isArrow()) {
float dx = segment.xEnd() - segment.xStart();
float dy = segment.yEnd() - segment.yStart();
drawArrow(ctx, r, segment.xEnd(), segment.yEnd(), dx, dy, segment.color());
}
}
void PlotView::traceRect(KDContext * ctx, KDRect r, PlotStore::Rect rect) const {