[python] matplotlib: Add a parameter to to arrow to set the arrow width

This commit is contained in:
Émilie Feral
2020-05-19 09:32:35 +02:00
parent e92b56b78e
commit 0f8f82b94b
4 changed files with 15 additions and 11 deletions

View File

@@ -89,16 +89,20 @@ void modpyplot_flush_used_heap() {
}
}
/* arrow(x,y,dx,dy, color)
/* arrow(x,y,dx,dy, head_width, color)
* x, y, dx, dy scalars
* */
mp_obj_t modpyplot_arrow(size_t n_args, const mp_obj_t *args) {
assert(n_args >= 4);
assert(sPlotStore != nullptr);
mp_obj_t arrowWidth = mp_obj_new_float(0.003); // Default value
if (n_args >= 5) {
arrowWidth = args[4];
}
KDColor color = colorFromOptionalArgumentAtIndex(n_args, args, 4);
sPlotStore->addSegment(args[0], args[1], mp_obj_float_binary_op(MP_BINARY_OP_INPLACE_ADD, mp_obj_get_float(args[0]), args[2]), mp_obj_float_binary_op(MP_BINARY_OP_INPLACE_ADD, mp_obj_get_float(args[1]), args[3]), color, true);
KDColor color = colorFromOptionalArgumentAtIndex(n_args, args, 5);
sPlotStore->addSegment(args[0], args[1], mp_obj_float_binary_op(MP_BINARY_OP_INPLACE_ADD, mp_obj_get_float(args[0]), args[2]), mp_obj_float_binary_op(MP_BINARY_OP_INPLACE_ADD, mp_obj_get_float(args[1]), args[3]), color, arrowWidth);
return mp_const_none;
}
@@ -359,7 +363,7 @@ mp_obj_t modpyplot_plot(size_t n_args, const mp_obj_t *args) {
KDColor color = colorFromOptionalArgumentAtIndex(n_args, args, 2);
for (int i=0; i<(int)length-1; i++) {
sPlotStore->addSegment(xItems[i], yItems[i], xItems[i+1], yItems[i+1], color, false);
sPlotStore->addSegment(xItems[i], yItems[i], xItems[i+1], yItems[i+1], color);
}
return mp_const_none;

View File

@@ -104,12 +104,12 @@ PlotStore::Segment::Segment(mp_obj_t tuple) {
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;
m_arrowWidth = mp_obj_get_float(elements[5]);
}
void PlotStore::addSegment(mp_obj_t xStart, mp_obj_t yStart, mp_obj_t xEnd, mp_obj_t yEnd, KDColor c, bool arrowEdge) {
void PlotStore::addSegment(mp_obj_t xStart, mp_obj_t yStart, mp_obj_t xEnd, mp_obj_t yEnd, KDColor c, mp_obj_t arrowWidth) {
mp_obj_t color = mp_obj_new_int(c);
mp_obj_t items[6] = {xStart, yStart, xEnd, yEnd, color, arrowEdge ? mp_const_true : mp_const_false};
mp_obj_t items[6] = {xStart, yStart, xEnd, yEnd, color, arrowWidth};
checkFloatType(items, 4);
mp_obj_t tuple = mp_obj_new_tuple(6, items);
mp_obj_list_append(m_segments, tuple);

View File

@@ -68,18 +68,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; }
float arrowWidth() const { return m_arrowWidth; }
KDColor color() const { return m_color; }
private:
float m_xStart;
float m_yStart;
float m_xEnd;
float m_yEnd;
bool m_arrow;
float m_arrowWidth;
KDColor m_color;
};
void addSegment(mp_obj_t xStart, mp_obj_t yStart, mp_obj_t xEnd, mp_obj_t yEnd, KDColor c, bool arrowEdge);
void addSegment(mp_obj_t xStart, mp_obj_t yStart, mp_obj_t xEnd, mp_obj_t yEnd, KDColor c, mp_obj_t arrowWidth = mp_obj_new_float(0.0));
Iterable<ListIterator<Segment>> segments() { return Iterable<ListIterator<Segment>>(m_segments); }
// Rect

View File

@@ -46,7 +46,7 @@ void PlotView::traceSegment(KDContext * ctx, KDRect r, PlotStore::Segment segmen
segment.xEnd(), segment.yEnd(),
segment.color()
);
if (segment.isArrow()) {
if (segment.arrowWidth() > 0.0f) {
float dx = segment.xEnd() - segment.xStart();
float dy = segment.yEnd() - segment.yStart();
drawArrow(ctx, r, segment.xEnd(), segment.yEnd(), dx, dy, segment.color());