# How to draw a triangle like this one

I have a rectangle of size $x \times n$ and need to draw the line that bisects the corners as percentages of the rectangle. The example plot shows a range from 0% to 100% in 5% increments. Can this be done in Mathematica? • I'm not at a computer right now, but at first blush, I'd probably plot an appropriately scaled Floor[] function and use the Filling -> Axis option. Constructing a direct Polygon[], would be most efficient, but will require programming effort. – J. M. is in limbo Feb 26 '16 at 5:04
• Quite a few ways to do this. Have a loot at DiscretePlot, also plotting unit steps or alternatively points with interpolation order->0 and the last two examples with filling to the axis. There should be examples of these here. – Mike Honeychurch Feb 26 '16 at 5:04

BarChart[
Range[0, 100, 5],
BarSpacing -> None,
ChartStyle -> Directive[EdgeForm[None], FaceForm[Black]],
Axes -> False
] Many more alternative approaches are available. For instance, you can build the graphics directives yourself, as J.M. suggested:

Graphics[
Table[Rectangle[{n, 0}, {(n + 5), n}], {n, 0, 95, 5}],
AspectRatio -> 1/GoldenRatio
]


... or use DiscretePlot, as Mike suggested:

DiscretePlot[x, {x, 0, 100, 5},
ExtentSize -> Left,
PlotStyle -> Directive[PointSize, Black],
FillingStyle -> Directive[Opacity, Black],
Axes -> False
]

• The Graphics solution worked well. Discrete plot adds a black line where 0 should be. Thanks! – R Hall Feb 26 '16 at 13:12
• @RHall Yes DiscretePlot will draw an extent line even at zero; I also found that the extent line seems to slightly "overhang" the bar it corresponds to. It is the least visually pleasing result. BarChart seems to avoid that. Thanks for the accept as well! – MarcoB Feb 26 '16 at 13:19

Here is a solution done with the help of Riffle[] and ListLinePlot[] down = {0, 0} + # {1.5, 1} & /@ Range[0, 10];
up = {0, 1} + # {1.5, 1} & /@ Range[0, 9];
ListLinePlot[Riffle[down, up], Filling -> Axis] Another simple method I think is using Floor[] directly.

Plot[Floor[x], {x, 0, 15}, Filling -> Axis] ListStepPlot[Range[0, 100, 5], Filling -> Axis, FillingStyle -> Black,
PlotStyle -> Black, Ticks -> None, Axes -> False] Here's my take:

raggedTriangle[pmin_?VectorQ, pmax_?VectorQ, p_: 5] :=
Module[{d = pmax - pmin, iv}, iv = Subdivide[#, 100/p] & /@ d;
Polygon[Append[TranslationTransform[pmin] /@
Most[Riffle[Transpose[iv],
Transpose[MapAt[RotateLeft, iv, 2]]]],
{First[pmax], Last[pmin]}]]]


An example:

Graphics[{{Blue, Rectangle[{1, 2}, {5, 3}]},
{Orange, raggedTriangle[{1, 2}, {5, 3}, 10]}}] • I get the error "Transpose::nmtx: The first two levels of {Subdivide[4,10],Subdivide[1,10]} cannot be transposed. In both 9 & 10 – R Hall Feb 26 '16 at 13:15
• Subdivide[] first became available in version 10.1. A substitute for earlier versions would be subdivide[xmax_: 1, n_Integer?Positive] := xmax Range[0, 1, 1/n]. – J. M. is in limbo Feb 26 '16 at 13:20
• Thanks @J.M. Looks like I'll need to upgrade – R Hall Feb 26 '16 at 13:51
• Nevertheless, if you make the substitution I indicated, then there's no need for you to upgrade yet just for this simple thing. ;) Upgrade only if you want all the other neat stuff that came after 10. – J. M. is in limbo Feb 26 '16 at 13:59