小编Hel*_*ena的帖子

Python:检查点是否在多边形内

我有一个描述Point的类(有2个坐标x和y)和一个描述Polygon的类,它有一个对应于角的列表(self.corners)我需要检查Point是否在多边形中

这是应该检查多边形中的Point in的函数.我正在使用Ray Casting Method

def in_me(self, point):
        result = False
        n = len(self.corners)
        p1x = int(self.corners[0].x)
        p1y = int(self.corners[0].y)
        for i in range(n+1):
            p2x = int(self.corners[i % n].x)
            p2y = int(self.corners[i % n].y)
            if point.y > min(p1y,p2y):
                if point.x <= max(p1x,p2x):
                    if p1y != p2y:
                        xinters = (point.y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x
                        print xinters
                    if p1x == p2x or point.x <= xinters:
                        result = not result
            p1x,p1y = p2x,p2y
         return result
Run Code Online (Sandbox Code Playgroud)

我运行了一个具有以下形状和点的测试:

PG1 = (0,0), (0,2), (2,2), (2,0)
point = (1,1)
Run Code Online (Sandbox Code Playgroud)

即使该行内的点,该脚本也会愉快地返回False.我无法找到错误

python scripting point polygon

39
推荐指数
2
解决办法
5万
查看次数

Ajax和Places API获取请求

我无法向Google Places API提供AJAX请求.码:

$.ajax({
    type: 'GET',
    url:'http://maps.googleapis.com/maps/api/place/textsearch/json?',
    dataType: 'json',
    data: {
        'query' : "restaurants+in+" + cityname,
        'key' : MyPublicKey,
        'sensor' : "false"
    },
    success: function(restaurans) {
        console.log(restaurants);
    }
});
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

ajax jquery json google-places-api

5
推荐指数
1
解决办法
2523
查看次数

Python和NLTK:如何分析句子语法?

我有这个代码,它应该根据定义的语法显示句子的句法结构.但是它返回一个空的[].我错过了什么或做错了什么?

import nltk

grammar = nltk.parse_cfg("""
S -> NP VP 
PP -> P NP
NP -> Det N | Det N PP 
VP -> V NP | VP PP
N -> 'Kim' | 'Dana' | 'everyone'
V -> 'arrived' | 'left' |'cheered'
P -> 'or' | 'and'
""")

def main():
    sent = "Kim arrived or Dana left and everyone cheered".split()
    parser = nltk.ChartParser(grammar)
    trees = parser.nbest_parse(sent)
    for tree in trees:
        print tree

if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

tree nlp nltk python-2.7

5
推荐指数
2
解决办法
4987
查看次数

Python:ValueError:int()的无效文字,基数为10:''错误

我有一个代码从文件中读取信息(行描述点,多边形,线和圆)并将其解析为根据类.Point有x和7坐标,Line有一个起点和一个终点.

我有一个列表(line = ['L1','L((1,1), (1,2))','# comment']),我试着把它变成一行.问题在于创建终点,在执行时我从ValueError: invalid literal for int() with base 10: ''变量中得到以下错误x2

问题是什么?

码:

def make_line(line):
        name = line[0]
        point = line[1].split(", ")
        p = point[0].split(",")
        x1 = int(p[0][3:])
        y1 = int(p[1][:-1])
        point1 = Point(x1,y1)
        p = point[1].split(",")
        x2 = int(p[0][1:])
        y2 = int(p[1][:-2])
        point2 = Point(x2,y2)
        line = Line(point1,point2)
        shapes[name] = line
Run Code Online (Sandbox Code Playgroud)

python int list

1
推荐指数
1
解决办法
8434
查看次数

标签 统计

python ×2

ajax ×1

google-places-api ×1

int ×1

jquery ×1

json ×1

list ×1

nlp ×1

nltk ×1

point ×1

polygon ×1

python-2.7 ×1

scripting ×1

tree ×1