-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
km:0.1.0 (#736)
Toto
authored
Jun 18, 2024
1 parent
031e598
commit 38fe94a
Showing
7 changed files
with
268 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
README.pdf | ||
lib.pdf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 E8D08F | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# `km` | ||
|
||
Draw simple Karnaugh maps. Use with: | ||
|
||
```typ | ||
#import "@preview/km:0.1.0": karnaugh | ||
#karnaugh(("C", "AB"), | ||
implicants: ( | ||
(0, 1, 1, 2), | ||
(1, 2, 2, 1), | ||
), | ||
( | ||
(0, 1, 0, 0), | ||
(0, 1, 1, 1), | ||
) | ||
) | ||
``` | ||
|
||
Samples are available in [`sample.pdf`](https://github.com/typst/packages/blob/main/packages/preview/km/0.1.0/sample.pdf). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#let binary-digits(x, digit: 0) = { | ||
let digits = range(digit).map(_ => 0) | ||
|
||
while (x != 0) { | ||
digits.insert(digit, calc.rem-euclid(x, 2)) | ||
x = calc.div-euclid(x, 2) | ||
} | ||
|
||
return digits.rev().slice(0, digit).rev() | ||
} | ||
|
||
#let gray-code(x) = x.bit-xor(x.bit-rshift(1)) | ||
|
||
#let karnaugh(labels, minterms, implicants: (), | ||
show-zero: false, mode: "code") = { | ||
let (X, Y) = range(2).map(i => | ||
if (type(labels.at(i)) == str) { | ||
// Label extracted from character(s) | ||
labels.at(i).clusters().map(x => eval(x, mode: "math")) | ||
} else { labels.at(i) } | ||
) | ||
|
||
// Dimensions: 2^len | ||
let row = calc.pow(2, X.len()) | ||
let column = calc.pow(2, Y.len()) | ||
let cell = ( | ||
width: 2.4em, | ||
height: 2.4em, | ||
) | ||
|
||
let gray-codes(till, digit) = range(till).map(i => gray-code(i)).map(code => | ||
block(width: cell.width, height: cell.height, inset: cell.height / 9, | ||
for digit in binary-digits(code, digit: digit) { | ||
math.equation[#digit] | ||
} | ||
) | ||
) | ||
|
||
return table( | ||
rows: row + 1, | ||
columns: column + 1, | ||
inset: 0em, | ||
stroke: (x, y) => | ||
if (x == 0 or y == 0) { none } else { 1pt }, | ||
align: (x, y) => { | ||
if (x == 0 and y != 0) { horizon + right } | ||
else if (x != 0 and y == 0) { center + bottom } | ||
else { horizon + center } | ||
}, | ||
// Top-left label block | ||
block(width: cell.width, height: cell.height, { | ||
line(end: (100%, 100%)) | ||
|
||
let label = ( | ||
x: box(for x in Y { x }), | ||
y: box(for y in X { y }), | ||
) | ||
|
||
context place(horizon + center, | ||
dx: measure(label.x).width / 2, dy: -0.5em, label.x) | ||
context place(horizon + center, | ||
dx: -measure(label.y).width / 2, dy: 0.5em, label.y) | ||
}), | ||
// Horizontal gray code labels | ||
..if mode == "code" { gray-codes(column, Y.len()) }, | ||
table.cell(x: 1, y: 1, | ||
rowspan: row, colspan: column, | ||
block(clip: true, { | ||
table( | ||
rows: row, | ||
columns: column, | ||
inset: 0em, | ||
align: horizon + center, | ||
stroke: 2pt / 3, | ||
..range(row).map(i => | ||
range(column).map(j => | ||
block(width: cell.width, height: cell.height, { | ||
let term = minterms.at(i).at(j) | ||
|
||
if type(term) == int { | ||
if term == 0 { | ||
if show-zero { $0$ } | ||
} else if term == 1 { $1$ } | ||
else { math.ast.small } | ||
} else { term } | ||
}) | ||
) | ||
).flatten() | ||
) | ||
|
||
while (implicants.len() > 0) { | ||
let implicant = implicants.pop() | ||
let (x, y, width, ..) = implicant | ||
let height = if implicant.len() == 3 { width } else { implicant.at(3) } | ||
|
||
place( | ||
top + left, | ||
dx: y * cell.width, | ||
dy: x * cell.height, | ||
rect( | ||
fill: rgb("#0003"), | ||
stroke: 2pt / 3, | ||
width: cell.width * width, | ||
height: cell.height * height, | ||
outset: -cell.height / 9) | ||
) | ||
|
||
// No need to expand out-of-range rectanges from the expanded rectange | ||
if (implicant.len() == 5 and not implicant.at(4)) { continue } | ||
|
||
// Draw extra out-of-range rectange(s) | ||
if x + height > row { | ||
implicants.push((row - x - height, y, width, height, false)) | ||
if y + width > column { | ||
implicants.push((row - x - height, column - y - width, width, height, false)) | ||
} | ||
} | ||
if y + width > column { | ||
implicants.push((x, column - y - width, width, height, false)) | ||
} | ||
} | ||
}) | ||
), | ||
..if mode == "code" { gray-codes(row, X.len()) }, | ||
) | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#import "./lib.typ": karnaugh | ||
|
||
|
||
#set text(font: "Kabel", weight: "bold") | ||
#show raw: set text(weight: "regular") | ||
#show math.equation: set text(font: "New Computer Modern Math") | ||
|
||
#align(center)[ | ||
= Samples of Karnaugh–Veitch Maps | ||
] | ||
|
||
#let descriptions = ( | ||
"Fig. 1. Map of an incompletely specified function", | ||
"Fig. 2. Zeros are not ignored", | ||
"Fig. 3. Crossing implicant" | ||
) | ||
|
||
#set figure(supplement: none) | ||
|
||
#let samples = ( | ||
``` | ||
import "./lib.typ": karnaugh | ||
let x = -1 | ||
karnaugh(("AB", "CD"), // Labels in string | ||
implicants: ( | ||
// (x, y, length/width, optional height) | ||
(1, 1, 2), | ||
(2, 0, 2) | ||
), | ||
( | ||
// Any number that is neither 0 or 1 will | ||
// be treated as don't-care. | ||
(0, 0, 0, x), | ||
(0, 1, x, 0), | ||
(1, 1, 1, 0), | ||
(x, 1, 0, x), | ||
) | ||
) | ||
```, | ||
``` | ||
import "./lib.typ": karnaugh | ||
karnaugh(("C", "AB"), | ||
implicants: ( | ||
(0, 1, 1, 2), | ||
(1, 2, 2, 1), | ||
), | ||
( | ||
(0, 1, 0, 0), | ||
(0, 1, 1, 1), | ||
), | ||
// Allow zero to appear | ||
show-zero: true | ||
) | ||
```, | ||
``` | ||
import "./lib.typ": karnaugh | ||
karnaugh( | ||
( | ||
// Labels in math mode | ||
($I_2$,), ($I_1$, $I_0$) | ||
), | ||
implicants: ( | ||
// `(0, 3, 2)` moves outside the frame | ||
// but will come back | ||
(0, 3, 2), | ||
(1, 0, 4, 1), | ||
), | ||
( | ||
(1, 0, 0, 1), | ||
(1, 1, 1, 1), | ||
) | ||
) | ||
``` | ||
) | ||
|
||
#for i in range(samples.len()) [ | ||
#grid( | ||
columns: (1fr, auto), | ||
align: horizon, | ||
raw(lang: "typc", samples.at(i).text.split("\n").slice(1).join("\n")), | ||
figure( | ||
eval(samples.at(i).text), | ||
caption: descriptions.at(i) | ||
) | ||
) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "km" | ||
version = "0.1.0" | ||
entrypoint = "lib.typ" | ||
authors = ["Toto <[email protected]>"] | ||
license = "MIT" | ||
description = "Draw simple Karnaugh maps" | ||
repository = "https://git.sr.ht/~toto/karnaugh" | ||
keywords = ["logic", "circuit"] | ||
categories = ["utility"] | ||
disciplines = ["mathematics"] | ||
compiler = "0.11.0" | ||
exclude = ["sample.pdf", "sample.typ", "README.typ"] |