python数据插补_Python直线插补

使用python做直线插补,初略计算插补出的像素点个数。

import turtle

point = []

# 判断直线类型

def judge_quadrant(x0, y0, xe, ye):

x = xe - x0

y = ye - y0

if (x > 0) and (y > 0): # 第一象限

quadrant = 1

elif (x < 0) and (y > 0): # 第二象限

quadrant = 2

elif (x < 0) and (y < 0): # 第三象限

quadrant = 3

elif (x > 0) and (y < 0): # 第四象限

quadrant = 4

elif x > 0 and y == 0:

quadrant = 5 # X轴正方向

elif x < 0 and y == 0:

quadrant = 6 # X轴负方向

elif x == 0 and y > 0:

quadrant = 7 # Y轴正方向

elif x == 0 and y < 0:

quadrant = 8 # Y轴负方向

return quadrant

# 插补确定像素点坐标

def interpolation(quadrant, x0, y0, xe, ye):

x = xe - x0 # x像素差

y = ye - y0 # y像素差

fm = 0

x1 = abs(x) # x方向步进数

y1 = abs(y) # y方向步进数

total = x1 + y1 # 总步进数

print("像素点数量 = ", total)

cnt = 0

while cnt < total:

if quadrant < 5:

if fm >= 0:

if quadrant == 1 or quadrant == 4:

x0 += 1

elif quadrant == 2 or quadrant == 3:

x0 -= 1

# point.append(p)

fm = fm - y1

elif fm < 0:

if quadrant == 1 or quadrant == 2:

y0 += 1

elif quadrant == 3 or quadrant == 4:

y0 -= 1

# point.append(p)

fm = fm + x1

else:

if quadrant == 5:

x0 += 1

elif quadrant == 6:

x0 -= 1

elif quadrant == 7:

y0 += 1

elif quadrant == 8:

y0 -= 1

p = (x0, y0)

point.append(p)

cnt += 1

print("点的列表为:", point)

def get_points():

line_path = [(300, 200), (20, 80)] # 定义直线端点坐标

for i in range(0, len(line_path) - 1):

xo = line_path[i][0]

yo = line_path[i][1]

print("起点:({}, {})".format(xo, yo))

xe = line_path[i + 1][0]

ye = line_path[i + 1][1]

print("终点:({}, {})".format(xe, ye))

quadrant = judge_quadrant(xo, yo, xe, ye)

print("画图方向在第{}象限。".format(quadrant))

interpolation(quadrant, xo, yo, xe, ye)

def draw_line():

print(point)

turtle.goto(point[0])

turtle.pendown()

for item in point:

turtle.goto(item)

def draw_coordinate():

turtle.penup()

turtle.goto(0, 500)

turtle.pendown()

turtle.goto(0, -1000)

turtle.penup()

turtle.goto(500, 0)

turtle.pendown()

turtle.goto(-500, 0)

turtle.penup()

turtle.goto(0, 0)

def main():

draw_coordinate()

get_points()

draw_line()

turtle.exitonclick()

if __name__ == '__main__':

main()

程序输出:

起点:(300, 200)

终点:(20, 80)

画图方向在第3象限。

像素点数量 = 400

点的列表为: [(299, 200), (299, 199), (298, 199), (297, 199), (297, 198), (296, 198), (295, 198), (295, 197), (294, 197), (293, 197), (292, 197), (292, 196), (291, 196), (290, 196), (290, 195), (289, 195), (288, 195), (288, 194), (287, 194), (286, 194), (285, 194), (285, 193), (284, 193), (283, 193), (283, 192), (282, 192), (281, 192), (281, 191), (280, 191), (279, 191), (278, 191), (278, 190), (277, 190), (276, 190), (276, 189), (275, 189), (274, 189), (274, 188), (273, 188), (272, 188), (271, 188), (271, 187), (270, 187), (269, 187), (269, 186), (268, 186), (267, 186), (267, 185), (266, 185), (265, 185), (264, 185), (264, 184), (263, 184), (262, 184), (262, 183), (261, 183), (260, 183), (260, 182), (259, 182), (258, 182), (257, 182), (257, 181), (256, 181), (255, 181), (255, 180), (254, 180), (253, 180), (253, 179), (252, 179), (251, 179), (250, 179), (250, 178), (249, 178), (248, 178), (248, 177), (247, 177), (246, 177), (246, 176), (245, 176), (244, 176), (243, 176), (243, 175), (242, 175), (241, 175), (241, 174), (240, 174), (239, 174), (239, 173), (238, 173), (237, 173), (236, 173), (236, 172), (235, 172), (234, 172), (234, 171), (233, 171), (232, 171), (232, 170), (231, 170), (230, 170), (229, 170), (229, 169), (228, 169), (227, 169), (227, 168), (226, 168), (225, 168), (225, 167), (224, 167), (223, 167), (222, 167), (222, 166), (221, 166), (220, 166), (220, 165), (219, 165), (218, 165), (218, 164), (217, 164), (216, 164), (215, 164), (215, 163), (214, 163), (213, 163), (213, 162), (212, 162), (211, 162), (211, 161), (210, 161), (209, 161), (208, 161), (208, 160), (207, 160), (206, 160), (206, 159), (205, 159), (204, 159), (204, 158), (203, 158), (202, 158), (201, 158), (201, 157), (200, 157), (199, 157), (199, 156), (198, 156), (197, 156), (197, 155), (196, 155), (195, 155), (194, 155), (194, 154), (193, 154), (192, 154), (192, 153), (191, 153), (190, 153), (190, 152), (189, 152), (188, 152), (187, 152), (187, 151), (186, 151), (185, 151), (185, 150), (184, 150), (183, 150), (183, 149), (182, 149), (181, 149), (180, 149), (180, 148), (179, 148), (178, 148), (178, 147), (177, 147), (176, 147), (176, 146), (175, 146), (174, 146), (173, 146), (173, 145), (172, 145), (171, 145), (171, 144), (170, 144), (169, 144), (169, 143), (168, 143), (167, 143), (166, 143), (166, 142), (165, 142), (164, 142), (164, 141), (163, 141), (162, 141), (162, 140), (161, 140), (160, 140), (159, 140), (159, 139), (158, 139), (157, 139), (157, 138), (156, 138), (155, 138), (155, 137), (154, 137), (153, 137), (152, 137), (152, 136), (151, 136), (150, 136), (150, 135), (149, 135), (148, 135), (148, 134), (147, 134), (146, 134), (145, 134), (145, 133), (144, 133), (143, 133), (143, 132), (142, 132), (141, 132), (141, 131), (140, 131), (139, 131), (138, 131), (138, 130), (137, 130), (136, 130), (136, 129), (135, 129), (134, 129), (134, 128), (133, 128), (132, 128), (131, 128), (131, 127), (130, 127), (129, 127), (129, 126), (128, 126), (127, 126), (127, 125), (126, 125), (125, 125), (124, 125), (124, 124), (123, 124), (122, 124), (122, 123), (121, 123), (120, 123), (120, 122), (119, 122), (118, 122), (117, 122), (117, 121), (116, 121), (115, 121), (115, 120), (114, 120), (113, 120), (113, 119), (112, 119), (111, 119), (110, 119), (110, 118), (109, 118), (108, 118), (108, 117), (107, 117), (106, 117), (106, 116), (105, 116), (104, 116), (103, 116), (103, 115), (102, 115), (101, 115), (101, 114), (100, 114), (99, 114), (99, 113), (98, 113), (97, 113), (96, 113), (96, 112), (95, 112), (94, 112), (94, 111), (93, 111), (92, 111), (92, 110), (91, 110), (90, 110), (89, 110), (89, 109), (88, 109), (87, 109), (87, 108), (86, 108), (85, 108), (85, 107), (84, 107), (83, 107), (82, 107), (82, 106), (81, 106), (80, 106), (80, 105), (79, 105), (78, 105), (78, 104), (77, 104), (76, 104), (75, 104), (75, 103), (74, 103), (73, 103), (73, 102), (72, 102), (71, 102), (71, 101), (70, 101), (69, 101), (68, 101), (68, 100), (67, 100), (66, 100), (66, 99), (65, 99), (64, 99), (64, 98), (63, 98), (62, 98), (61, 98), (61, 97), (60, 97), (59, 97), (59, 96), (58, 96), (57, 96), (57, 95), (56, 95), (55, 95), (54, 95), (54, 94), (53, 94), (52, 94), (52, 93), (51, 93), (50, 93), (50, 92), (49, 92), (48, 92), (47, 92), (47, 91), (46, 91), (45, 91), (45, 90), (44, 90), (43, 90), (43, 89), (42, 89), (41, 89), (40, 89), (40, 88), (39, 88), (38, 88), (38, 87), (37, 87), (36, 87), (36, 86), (35, 86), (34, 86), (33, 86), (33, 85), (32, 85), (31, 85), (31, 84), (30, 84), (29, 84), (29, 83), (28, 83), (27, 83), (26, 83), (26, 82), (25, 82), (24, 82), (24, 81), (23, 81), (22, 81), (22, 80), (21, 80), (20, 80)]

[(299, 200), (299, 199), (298, 199), (297, 199), (297, 198), (296, 198), (295, 198), (295, 197), (294, 197), (293, 197), (292, 197), (292, 196), (291, 196), (290, 196), (290, 195), (289, 195), (288, 195), (288, 194), (287, 194), (286, 194), (285, 194), (285, 193), (284, 193), (283, 193), (283, 192), (282, 192), (281, 192), (281, 191), (280, 191), (279, 191), (278, 191), (278, 190), (277, 190), (276, 190), (276, 189), (275, 189), (274, 189), (274, 188), (273, 188), (272, 188), (271, 188), (271, 187), (270, 187), (269, 187), (269, 186), (268, 186), (267, 186), (267, 185), (266, 185), (265, 185), (264, 185), (264, 184), (263, 184), (262, 184), (262, 183), (261, 183), (260, 183), (260, 182), (259, 182), (258, 182), (257, 182), (257, 181), (256, 181), (255, 181), (255, 180), (254, 180), (253, 180), (253, 179), (252, 179), (251, 179), (250, 179), (250, 178), (249, 178), (248, 178), (248, 177), (247, 177), (246, 177), (246, 176), (245, 176), (244, 176), (243, 176), (243, 175), (242, 175), (241, 175), (241, 174), (240, 174), (239, 174), (239, 173), (238, 173), (237, 173), (236, 173), (236, 172), (235, 172), (234, 172), (234, 171), (233, 171), (232, 171), (232, 170), (231, 170), (230, 170), (229, 170), (229, 169), (228, 169), (227, 169), (227, 168), (226, 168), (225, 168), (225, 167), (224, 167), (223, 167), (222, 167), (222, 166), (221, 166), (220, 166), (220, 165), (219, 165), (218, 165), (218, 164), (217, 164), (216, 164), (215, 164), (215, 163), (214, 163), (213, 163), (213, 162), (212, 162), (211, 162), (211, 161), (210, 161), (209, 161), (208, 161), (208, 160), (207, 160), (206, 160), (206, 159), (205, 159), (204, 159), (204, 158), (203, 158), (202, 158), (201, 158), (201, 157), (200, 157), (199, 157), (199, 156), (198, 156), (197, 156), (197, 155), (196, 155), (195, 155), (194, 155), (194, 154), (193, 154), (192, 154), (192, 153), (191, 153), (190, 153), (190, 152), (189, 152), (188, 152), (187, 152), (187, 151), (186, 151), (185, 151), (185, 150), (184, 150), (183, 150), (183, 149), (182, 149), (181, 149), (180, 149), (180, 148), (179, 148), (178, 148), (178, 147), (177, 147), (176, 147), (176, 146), (175, 146), (174, 146), (173, 146), (173, 145), (172, 145), (171, 145), (171, 144), (170, 144), (169, 144), (169, 143), (168, 143), (167, 143), (166, 143), (166, 142), (165, 142), (164, 142), (164, 141), (163, 141), (162, 141), (162, 140), (161, 140), (160, 140), (159, 140), (159, 139), (158, 139), (157, 139), (157, 138), (156, 138), (155, 138), (155, 137), (154, 137), (153, 137), (152, 137), (152, 136), (151, 136), (150, 136), (150, 135), (149, 135), (148, 135), (148, 134), (147, 134), (146, 134), (145, 134), (145, 133), (144, 133), (143, 133), (143, 132), (142, 132), (141, 132), (141, 131), (140, 131), (139, 131), (138, 131), (138, 130), (137, 130), (136, 130), (136, 129), (135, 129), (134, 129), (134, 128), (133, 128), (132, 128), (131, 128), (131, 127), (130, 127), (129, 127), (129, 126), (128, 126), (127, 126), (127, 125), (126, 125), (125, 125), (124, 125), (124, 124), (123, 124), (122, 124), (122, 123), (121, 123), (120, 123), (120, 122), (119, 122), (118, 122), (117, 122), (117, 121), (116, 121), (115, 121), (115, 120), (114, 120), (113, 120), (113, 119), (112, 119), (111, 119), (110, 119), (110, 118), (109, 118), (108, 118), (108, 117), (107, 117), (106, 117), (106, 116), (105, 116), (104, 116), (103, 116), (103, 115), (102, 115), (101, 115), (101, 114), (100, 114), (99, 114), (99, 113), (98, 113), (97, 113), (96, 113), (96, 112), (95, 112), (94, 112), (94, 111), (93, 111), (92, 111), (92, 110), (91, 110), (90, 110), (89, 110), (89, 109), (88, 109), (87, 109), (87, 108), (86, 108), (85, 108), (85, 107), (84, 107), (83, 107), (82, 107), (82, 106), (81, 106), (80, 106), (80, 105), (79, 105), (78, 105), (78, 104), (77, 104), (76, 104), (75, 104), (75, 103), (74, 103), (73, 103), (73, 102), (72, 102), (71, 102), (71, 101), (70, 101), (69, 101), (68, 101), (68, 100), (67, 100), (66, 100), (66, 99), (65, 99), (64, 99), (64, 98), (63, 98), (62, 98), (61, 98), (61, 97), (60, 97), (59, 97), (59, 96), (58, 96), (57, 96), (57, 95), (56, 95), (55, 95), (54, 95), (54, 94), (53, 94), (52, 94), (52, 93), (51, 93), (50, 93), (50, 92), (49, 92), (48, 92), (47, 92), (47, 91), (46, 91), (45, 91), (45, 90), (44, 90), (43, 90), (43, 89), (42, 89), (41, 89), (40, 89), (40, 88), (39, 88), (38, 88), (38, 87), (37, 87), (36, 87), (36, 86), (35, 86), (34, 86), (33, 86), (33, 85), (32, 85), (31, 85), (31, 84), (30, 84), (29, 84), (29, 83), (28, 83), (27, 83), (26, 83), (26, 82), (25, 82), (24, 82), (24, 81), (23, 81), (22, 81), (22, 80), (21, 80), (20, 80)]

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值