Python
- 46 ответов
- 0 вопросов
39
Вклад в тег
x1,y1───────────┐
│ │
│ │
│ │
│ │
└───────────x2,y2
Прямоугольники (0,3,4,0), (4,7,7,3) и (7,3,10,0) будут выглядеть примерно так:
* * *
* * *
* * *
* * *
* * * * * * *
* * * * * * *
* * * * * * *
Рассмотрим три прямоугольника (0,3,4,0), (2,5,5,1) и (4,7,7,4):
* * *
* * *
* * + * *
* * *
* * + + *
* * + + *
* * * *
Рассмотрим прямоугольники (0,3,4,0), (2,5,5,1) и (3,3,0,6)
* * *
* * *
* * + x + *
* * + x + *
* * * + * *
A = (12 + 12 + 9) - (4 + 3 + 4) + (2) = 33 - 11 + 2 = 24
A = S1 - S2 + S3 - S4 + S5 - S6 + S7 - S8 + ...
S1 = 12 + 12 + 9
S2 = 4 + 3 + 4
S3 = 2
def bounding_box(rects):
return (min(r[0] for r in rects),
max(r[1] for r in rects),
max(r[2] for r in rects),
min(r[3] for r in rects))
def area(rect):
a, b, c, d = rect
return (c - a) * (b - d)
def clip(bb, rects):
if not rects:
return []
(x1, y1, x2, y2) = rects[0]
rs = rects[1:]
(a1, b1, a2, b2) = bb
if a1 == a2 or b1 == b2:
return []
if a1 >= x2 or a2 <= x1 or y1 <= b2 or y2 >= b1:
return clip(bb, rs)
result = [(max(a1, x1), min(b1, y1), min(a2, x2), max(b2, y2))]
return result + clip(bb, rs)
def cover(bb, rects):
if not rects:
return 0
rc = rects[0]
rs = rects[1:]
(a1, b1, a2, b2) = bb
(x1, y1, x2, y2) = rc
top = (a1, b1, a2, y1)
bottom = (a1, y2, a2, b2)
left = (a1, y1, x1, y2)
right = (x2, y1, a2, y2)
sum_area = sum(cover(x, clip(x, rs)) for x in [top, bottom, left, right])
return area(rc) + sum_area
def main():
width_canvas = 10
height_canvas = 10
rect1 = (0, 3, 4, 0)
rect2 = (2, 5, 5, 1)
rect3 = (3, 3, 6, 0)
rs = [rect1, rect2, rect3]
painted_area = cover(bounding_box(rs), rs)
canvas_area = width_canvas * height_canvas
unpainted_area = canvas_area - painted_area
print("Canvas area:", canvas_area)
print("Painted area:", painted_area)
print("Unpainted area:", unpainted_area)
if __name__ == '__main__':
main()
https://domain.name/data.json?searchtext=Запрос+На+Кириллице
package main
import (
"fmt"
"net/url"
)
func main() {
q := "Запрос+На+Кириллице"
u := "https://domain.name/data.json"
fmt.Println(u + "?searchtext=" + url.QueryEscape(q))
}
$ go run main.go
https://domain.name/data.json?searchtext=%D0%97%D0%B0%D0%BF%D1%80%D0%BE%D1%81%2B%D0%9D%D0%B0%2B%D0%9A%D0%B8%D1%80%D0%B8%D0%BB%D0%BB%D0%B8%D1%86%D0%B5
go get golang.org/x/net/idna
package main
import (
"fmt"
"golang.org/x/net/idna"
)
var p *idna.Profile
func main() {
p = idna.New()
fmt.Println(p.ToASCII("россия.рф"))
}
$ go run main.go
xn--h1alffa9f.xn--p1ai <nil>