[apps/shared] CurveView: add a parameter to drawSegment to draw dashed

segment
This commit is contained in:
Émilie Feral
2020-01-07 15:46:34 +01:00
committed by Léa Saviot
parent 39d65a1ad0
commit 818067e03b
3 changed files with 28 additions and 21 deletions

View File

@@ -19,8 +19,8 @@ void TrigonometryGraphView::drawRect(KDContext * ctx, KDRect rect) const {
drawCurve(ctx, rect, 0, 2.0f*M_PI, M_PI/180.0f, [](float t, void * model, void * context) {
return Poincare::Coordinate2D<float>(std::cos(t), std::sin(t));
}, nullptr, nullptr, true, Palette::GreyDark, false);
drawSegment(ctx, rect, Axis::Vertical, std::cos(m_model->angle()), 0.0f, std::sin(m_model->angle()), Palette::Red);
drawSegment(ctx, rect, Axis::Horizontal, std::sin(m_model->angle()), 0.0f, std::cos(m_model->angle()), Palette::Red);
drawSegment(ctx, rect, Axis::Vertical, std::cos(m_model->angle()), 0.0f, std::sin(m_model->angle()), Palette::Red, 1, 3);
drawSegment(ctx, rect, Axis::Horizontal, std::sin(m_model->angle()), 0.0f, std::cos(m_model->angle()), Palette::Red, 1, 3);
drawDot(ctx, rect, std::cos(m_model->angle()), std::sin(m_model->angle()), Palette::Red);
// TODO
}

View File

@@ -405,24 +405,32 @@ void CurveView::drawLine(KDContext * ctx, KDRect rect, Axis axis, float coordina
}
}
void CurveView::drawSegment(KDContext * ctx, KDRect rect, Axis axis, float coordinate, float lowerBound, float upperBound, KDColor color, KDCoordinate thickness) const {
KDRect lineRect = KDRectZero;
switch(axis) {
case Axis::Horizontal:
lineRect = KDRect(
std::round(floatToPixel(Axis::Horizontal, lowerBound)), std::round(floatToPixel(Axis::Vertical, coordinate)),
std::round(floatToPixel(Axis::Horizontal, upperBound)) - std::round(floatToPixel(Axis::Horizontal, lowerBound)), thickness
);
break;
case Axis::Vertical:
lineRect = KDRect(
std::round(floatToPixel(Axis::Horizontal, coordinate)), std::round(floatToPixel(Axis::Vertical, upperBound)),
thickness, std::round(floatToPixel(Axis::Vertical, lowerBound)) - std::round(floatToPixel(Axis::Vertical, upperBound))
);
break;
void CurveView::drawSegment(KDContext * ctx, KDRect rect, Axis axis, float coordinate, float lowerBound, float upperBound, KDColor color, KDCoordinate thickness, KDCoordinate dashSize) const {
KDCoordinate start = std::round(floatToPixel(axis, lowerBound));
KDCoordinate end = std::round(floatToPixel(axis, upperBound));
if (start > end) {
start = end;
end = std::round(floatToPixel(axis, lowerBound));
}
if (rect.intersects(lineRect)) {
ctx->fillRect(lineRect, color);
Axis otherAxis = (axis == Axis::Horizontal) ? Axis::Vertical : Axis::Horizontal;
KDCoordinate pixelCoordinate = std::round(floatToPixel(otherAxis, coordinate));
if (dashSize < 0) {
// Continuous segment is equivalent to one big dash
dashSize = end - start;
}
KDRect lineRect = KDRectZero;
for (KDCoordinate i = start; i < end; i += 2*dashSize) {
switch(axis) {
case Axis::Horizontal:
lineRect = KDRect(i, pixelCoordinate, dashSize, thickness);
break;
case Axis::Vertical:
lineRect = KDRect(pixelCoordinate, i, thickness, dashSize);
break;
}
if (rect.intersects(lineRect)) {
ctx->fillRect(lineRect, color);
}
}
}
@@ -444,7 +452,6 @@ const uint8_t oversizeDotMask[oversizeDotDiameter][oversizeDotDiameter] = {
{0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C},
{0x45, 0x0C, 0x00, 0x00, 0x00, 0x0C, 0x45},
{0xE1, 0x45, 0x0C, 0x00, 0x0C, 0x45, 0xE1},
};
void CurveView::drawDot(KDContext * ctx, KDRect rect, float x, float y, KDColor color, bool oversize) const {

View File

@@ -61,7 +61,7 @@ protected:
float coordinate, KDColor color, KDCoordinate thickness = 1) const;
void drawSegment(KDContext * ctx, KDRect rect, Axis axis,
float coordinate, float lowerBound, float upperBound,
KDColor color, KDCoordinate thickness = 1) const;
KDColor color, KDCoordinate thickness = 1, KDCoordinate dashSize = -1) const;
void drawDot(KDContext * ctx, KDRect rect, float x, float y, KDColor color, bool oversize = false) const;
void drawGrid(KDContext * ctx, KDRect rect) const;
void drawAxes(KDContext * ctx, KDRect rect) const;