python获取中文字体点阵坐标_在squ中高效在线获取点阵点
根据matovitch最新的代码草案(我只浏览了一眼),我可能会想得太多了,但无论如何。。。在
设A=(A.x,A.y),B=(B.x,B.y),其中(A.x,A.y,B.x,B.y)是整数。
然后直线p,AB的垂直平分线穿过
M=(M.x,M.y)=((A.x+B.x)/2,(A.y+B.y)/2)
AB和p的斜率乘积为-1,因此p的斜率为
-(B.x-A.x)/(B.y-A.y)
因此,在点斜率形式下,p的方程是
(y-M.y)/(x-M.x)=(A.x-B.x)/(B.y-A.y)
重新排列,
y*(B.y-A.y)+x*(B.x-A.x)=M.y*(B.y-A.y)+M.x*(B.x-A.x)
=((B.y+A.y)*(B.y-A.y)+(B.x+A.x)*(B.x-A.x))/2
=(B.y^2-A.y^2+B.x ^2-A.x^2)/2
显然,对于任何晶格点(x,y),y*(B.y-A.y)+x*(B.x-A.x)必须是整数。所以只有当(B.y^2-A.y^2+B.x^2-A.x^2)是偶数时,直线p才会通过晶格点。在
现在(B.y^2-A.y^2+B.x^2-A.x^2)是偶数的&仅当(A.x+B.x+A.y+B.y)是偶数,如果偶数(A.x,A.y,B.x,B.y)是奇数。在下面,我假设(A.x+B.x+A.y+B.y)是偶数。在
让
dx=(B.x-A.x)
dy=(B.y-A.y)
s=(B.y^2-A.y^2+B.x^2-A.x^2)/2
所以p的方程是
y*dy+x*dx=s
因为y,dy,x,dx&s都是整数,所以这个方程是一个线性丢番图方程,而求这类方程解的标准方法是使用extended Euclidean algorithm。我们的方程只有在dx&dy的最大公约数除以s时才有解。幸运的是,在这种情况下是正确的,但我不在这里给出证明。在
设Y,X是Y*dy+X*dx=g的解,其中g是gcd(dx,dy),即
Y*dy+X*dx=g
Y*dy/g+X*dx/g=1
设dy'=dy/g,dx'=dx/g,s'=s/g,所以
Y*dy'+X*dx'=1
将最后一个p的方程除以g,我们得到
y*dy'+x*dx'=s'
我们现在可以为它构造一个解决方案。
(Y*s')*dy'+(X*s')*dx'=s'
i、 e.,(X*s',Y*s')是直线上的点阵点。在
我们可以得到这样的所有解决方案:
(Y*s'+k*dx')*dy'+(X*s'-k*dy')*dx'=s',对于所有整数k
为了将解限制在(0,0)到(W,H)之间,我们需要解决k的这些不等式:
0<;=X*s'-k*dy'<;=W和0<;=Y*s'+k*dx'<;=H
我不会在这里展示这些不等式的解决方案;有关详细信息,请参阅下面的代码。在#! /usr/bin/env python
''' Lattice Line
Find lattice points, i.e, points with integer co-ordinates,
on the line that is the perpendicular bisector of the line segment AB,
where A & B are lattice points.
See http://stackoverflow.com/q/31265139/4014959
Written by PM 2Ring 2015.07.08
Code for Euclid's algorithm & the Diophantine solver written 2010.11.27
'''
from __future__ import division
import sys
from math import floor, ceil
class Point(object):
''' A simple 2D point '''
def __init__(self, x, y):
self.x, self.y = x, y
def __repr__(self):
return "Point(%s, %s)" % (self.x, self.y)
def __str__(self):
return "(%s, %s)" % (self.x, self.y)
def euclid(a, b):
''' Euclid's extended algorithm for the GCD.
Returns a list of tuples of (dividend, quotient, divisor, remainder)
'''
if a < b:
a, b = b, a
k = []
while True:
q, r = a // b, a % b
k.append((a, q, b, r))
if r == 0:
break
a, b = b, r
return k
def dio(aa, bb):
''' Linear Diophantine solver
Returns [x, aa, y, bb, d]: x*aa + y*bb = d
'''
a, b = abs(aa), abs(bb)
swap = a < b
if swap:
a, b = b, a
#Handle trivial cases
if a == b:
eqn = [2, a, -1, a]
elif a % b == 0:
q = a // b
eqn = [1, a, 1-q, b]
else:
#Generate quotients & remainders list
z = euclid(a, b)[::-1]
#Build equation from quotients & remainders
eqn = [0, 0, 1, 0]
for v in z[1:]:
eqn = [eqn[2], v[0], eqn[0] - eqn[2]*v[1], v[2]]
#Rearrange & fix signs, if required
if swap:
eqn = eqn[2:] + eqn[:2]
if aa < 0:
eqn[:2] = [-eqn[0], -eqn[1]]
if bb < 0:
eqn[2:] = [-eqn[2], -eqn[3]]
d = eqn[0]*eqn[1] + eqn[2]*eqn[3]
if d < 0:
eqn[0], eqn[2], d = -eqn[0], -eqn[2], -d
return eqn + [d]
def lattice_line(pA, pB, pC):
''' Find lattice points, i.e, points with integer co-ordinates, on
the line that is the perpendicular bisector of the line segment AB,
Only look for points in the rectangle from (0,0) to C
Let M be the midpoint of AB. Then M = ((A.x + B.x)/2, (A.y + B.y)/2),
and the equation of the perpendicular bisector of AB is
(y - M.y) / (x - M.x) = (A.x - B.x) / (B.y - A.y)
'''
nosolutions = 'No solutions found'
dx = pB.x - pA.x
dy = pB.y - pA.y
#Test parity of co-ords to see if there are solutions
if (dx + dy) % 2 == 1:
print nosolutions
return
#Handle horizontal & vertical lines
if dx == 0:
#AB is vertical, so bisector is horizontal
y = pB.y + pA.y
if dy == 0 or y % 2 == 1:
print nosolutions
return
y //= 2
for x in xrange(pC.x + 1):
print Point(x, y)
return
if dy == 0:
#AB is horizontal, so bisector is vertical
x = pB.x + pA.x
if x % 2 == 1:
print nosolutions
return
x //= 2
for y in xrange(pC.y + 1):
print Point(x, y)
return
#Compute s = ((pB.x + pA.x)*dx + (pB.y + pA.y)*dy) / 2
#s will always be an integer since (dx + dy) is even
#The desired line is y*dy + x*dx = s
s = (pB.x**2 - pA.x**2 + pB.y**2 - pA.y**2) // 2
#Find ex, ey, g: ex * dx + ey * dy = g, where g is the gcd of (dx, dy)
#Note that g also divides s
eqn = dio(dx, dy)
ex, ey, g = eqn[::2]
#Divide the parameters of the equation by the gcd
dx //= g
dy //= g
s //= g
#Find lattice limits
xlo = (ex * s - pC.x) / dy
xhi = ex * s / dy
if dy < 0:
xlo, xhi = xhi, xlo
ylo = -ey * s / dx
yhi = (pC.y - ey * s) / dx
if dx < 0:
ylo, yhi = yhi, ylo
klo = int(ceil(max(xlo, ylo)))
khi = int(floor(min(xhi, yhi)))
print 'Points'
for k in xrange(klo, khi + 1):
x = ex * s - dy * k
y = ey * s + dx * k
assert x*dx + y*dy == s
print Point(x, y)
def main():
if len(sys.argv) != 7:
print ''' Find lattice points, i.e, points with integer co-ordinates,
on the line that is the perpendicular bisector of the line segment AB,
where A & B are lattice points with co-ords (xA, yA) & (xB, yB).
Only print lattice points in the rectangle from (0, 0) to (W, H)
Usage:
%s xA yA xB yB W H''' % sys.argv[0]
exit(1)
coords = [int(s) for s in sys.argv[1:]]
pA = Point(*coords[0:2])
pB = Point(*coords[2:4])
pC = Point(*coords[4:6])
lattice_line(pA, pB, pC)
if __name__ == '__main__':
main()
我没有对这段代码进行过广泛的测试,但它似乎能正常工作。:)
python获取中文字体点阵坐标_在squ中高效在线获取点阵点相关推荐
- python获取中文字体点阵坐标_点阵汉字显示 - freecamel的个人空间 - OSCHINA - 中文开源技术交流社区...
目录: DOS下的点阵汉字 汉字的内码 汉字字模 汉字库文件 打印字库文件和HZK12 main() { unsigned char *s,*e="ABcd",*c="你 ...
- python获取中文字体点阵坐标_Python实现点阵字体读取与转换的方法
点阵字体是指根据文字的像素点来显示的字体,效果如下: 使用Python读取并显示的过程如下: 根据中文字符获取GB2312编码 通过GB2312编码计算该汉字在点阵字库中的区位和码位 通过区位和码位计 ...
- python获取中文字体点阵坐标_Python实现点阵字体读取与转换
点阵字体是指根据文字的像素点来显示的字体,效果如下: 使用Python读取并显示的过程如下: 根据中文字符获取GB2312编码 通过GB2312编码计算该汉字在点阵字库中的区位和码位 通过区位和码位计 ...
- python编辑器中文字体倒立的_如何用Python+人工识别处理知乎的倒立汉字验证码...
展开全部 # 登录知乎,通过保存验证图片方式 import urllib.request import urllib.parse import time import http.cookiejar w ...
- 永久解决python matplotlib 中文字体的显示乱码-Windows系统
在python中使用matplotlib绘图时,新手通常会遇到中文字体无法显示或显示乱码的问题,这是因为matplotlib中默认没有中文字体.windows系统下的永久解决方法如下: 1.确定当前p ...
- python中的字体英文名_获取中文字体的英文名字
(方法在分割线后面,前面叙事) 今天用了很久电脑,突然就觉得看着Windows下Chrome的字体觉得很不舒服,跟Mac下的差太远了,于是就开始折腾怎么设置浏览器字体. 先讲一下流程,我的操作方案是: ...
- python中文字体下载_解决Linux系统下python matplotlib中文字体显示问题
最近想学习一些python数据分析的内容,就弄了个爬虫爬取了一些数据,并打算用Anaconda一套的工具(pandas, numpy, scipy, matplotlib, jupyter)等进行一些 ...
- python中文字体下载_Python在Matplotlib图中显示中文字体的操作方法
1. 说明 本篇主要针对在Ubuntu系统中,matplotlib显示不了中文的问题,尤其是在无法安装系统字体的情况下,解决Python绘图时中文显示的问题. 2. 在系统中安装字体 $ fc-lis ...
- linux下python matplotlib 中文字体Font family [‘sans-serif‘] not found. Falling back to DejaVu Sans
问题 在linux新的python环境,安装了matplotlib,但是使用中文字体时找不到. 问题代码: import matplotlib.pyplot as plt plt.rcParams[' ...
最新文章
- POJ 3784.Running Median
- 【采用】信用风险模型(申请评分、行为评分)与数据准备(违约期限、WOE转化)
- golang递归获取目录下的所有文件
- 系统分析之100亿级日志系统是怎么设计出来的?
- 【theano-windows】学习笔记六——theano中的循环函数scan
- 8086 寻址方式_8086微处理器的不同寻址模式
- webform中提交按钮同时执行更新和插入操作_软件测试中的功能测试点(三)
- JAVA 基础之容器集合(Collection和Map)
- 【华为云技术分享】【昇腾】【玩转Atlas200DK系列】基于Pycharm专业版构建开发板python开发运行环境
- 修正IE6不支持position:fixed的bug
- cocos2d-x性能优化的那些事
- 深度学习自学(十七):caffe-sphereface-编译matcaffe遇到的问题
- 【LeetCode】【字符串】题号:*12. 整数转罗马数字
- java线程卖票_Java通过卖票理解多线程
- java计算某天到当前时间的间隔天数
- pyttsx3 语音包安装、使用详解
- 宏碁暗影骑士擎安装双系统踩坑记录
- 机器学习实战之决策树(一)构造决策树
- jupyter notebook ModuleNotFoundError: No module named ‘tensorflow‘---爬出坑
- 网络中国象棋小游戏的实现