| Bytes | Lang | Time | Link |
|---|---|---|---|
| 021 | MATL | 250905T115121Z | Luis Men |
| 116 | Charcoal | 250904T163437Z | Neil |
| nan | 250904T111452Z | lina mar |
MATL, 23 21 bytes
80:q40:qXgii4$ZQ!42*c
The input is two numeric vectors with the x and y coordinates respectively. The code outputs the full 80×40 text grid. Non-space characters are points that are strictly inside the polygon.
Try at MATL Online! Or verify all test cases (the footer code removes outer whitespace for easier visualization)
Charcoal, 116 bytes
B⁸⁰¦⁴⁰ FθJ§ι⁰§ι¹Fθ«≔⁻⊟ιⅉζ≔⁻⊟ιⅈηMηζ≔I§⎇›⁰ζ13¦75›⁰ηε≔⎇›↔ζ↔η⎇›⁰沦⁶∧›⁰η⁴δ≔↔⟦ηζ⟧η≔⌈ηζFζ«≧⁻⊗⌊ηζ⊞υ⎇›⁰ζεδ¿›⁰ζ≧⁺⊗⌈ηζ»»G✳✳υ²*
Try it online! Link is to verbose version of code. Explanation:
B⁸⁰¦⁴⁰
Create an 80×40 field.
FθJ§ι⁰§ι¹
Jump to the last point of the polygon. (This jumps to every point in turn, but that's because that's the golfiest way.)
Fθ«
Loop over the points of the polygon.
≔⁻⊟ιⅉζ≔⁻⊟ιⅈηMηζ
Get the relative offsets from the previous point, then move to that point.
≔I§⎇›⁰ζ13¦75›⁰ηε≔⎇›↔ζ↔η⎇›⁰沦⁶∧›⁰η⁴δ
Work out which Charcoal direction values correspond to the closest diagonal and orthogonal to the relative offset.
≔↔⟦ηζ⟧η≔⌈ηζ
Get the absolute offsets and take the maximum as both the number of steps of Bresenham's line algorithm but also the first error value.
Fζ«
Loop over each point on the line.
≧⁻⊗⌊ηζ
Update the error value.
⊞υ⎇›⁰ζεδ
Record a step in the appropriate direction.
¿›⁰ζ≧⁺⊗⌈ηζ
If a diagonal step was taken then update the error value again.
»»G✳✳υ²*
Draw a filled polygon of all the steps taken. (Note that the verbose source uses a capital S in the word Directions; this is due to a bug in the version of Charcoal on TIO.)
p=eval(input())
w,h=80,40
a=[[" "]*w for _ in range(h)]
for y in range(h):
r=[]
for (x1,y1),(x2,y2) in zip(p,p[1:]+p[:1]):
if (y1<=y<y2)or(y2<=y<y1):
r+=[x1+(y-y1)*(x2-x1)/(y2-y1)]
r.sort()
for x1,x2 in zip(r[::2],r[1::2]):a[y][int(x1):int(x2)+1]="*"*(int(x2)-int(x1)+1)
print("\n".join("".join(r)for r in a))
How it works
Reads list of points p (anticlockwise).
For each scanline y, computes intersections of polygon edges with that line.
Sorts intersections, fills between pairs with *.
Prints an 80x40 ASCII grid. Example
Input:
[[1,1],[1,3],[2,3],[2,1]]
Output:
**
**
**