15 May
2022
15 May
'22
3:39 p.m.
#include <stdio.h> int grid[5*5] = {0}; typedef struct pt { int x; int y; } pt; void plot(pt p) { grid[p.x+p.y*5] = 1; } pt pt1 = {2,1}; pt pt2 = {4,3}; pt pt3 = {0,3}; void line(pt a, pt b) { pt change = {b.x - a.x, b.y - a.y}; int ctx = change.x > 0 ? change.x : -change.x; int cty = change.y > 0 ? change.y : -change.y; int ct = ctx > cty ? ctx : cty; for (int t = 0; t <= ct; ++ t) { pt p = {a.x + change.x * t / ct, a.y + change.y * t / ct}; plot(p); } } int main() { line(pt1, pt2); line(pt2, pt3); line(pt3, pt1); for (int y = 0; y < 5; y ++) { for (int x = 0; x < 5; x ++) printf("%d", grid[x + y * 5]); printf("\n"); } }