两点之间的直线(straight line between two points)
在HTML画布上,我有从1到N的多个点,这基本上是连接点应用程序并在touchstart上激活。
有验证,以便它们只能连接1中的点并转到2(.. n)。 问题是,现在是没有验证该线是一条直线,我正在寻找一个算法来做到这一点,这是我到目前为止所想到的
- 对于2个点(x1,y1)到(x2,y2),通过找到斜率并使用公式y = mx + b得到所有坐标
- 在touchmove上获取x,y co-ornnates并确保它是前一步中的一个点并绘制一条线,否则不绘制线。
有没有更好的方法来做到这一点,或者我可以采取任何不同的方法?
On a HTML canvas I have multiple points starting from 1 to N, this is basically a connect the dots application and is activated on touchstart.
There is validation so that they can only connect the dots from 1 and go to 2 (.. n). The issue is that right now is there is no validation that the line is a straight line and I am looking for an algorithm to do this, Here is what I have thought so far
- For 2 points (x1,y1) to (x2,y2) get all the coordinates by finding the slope and using the formula y = mx + b
- on touchmove get the x,y co-oridnates and make sure it is one of the points from the earlier step and draw a line else do not draw the line.
Is there a better way to do this or are there any different approaches that I can take ?
原文:https://stackoverflow.com/questions/5918007
最满意答案
基本上你有字典词典的字典。 对于这种情况,它简单。 但是如果你有多个这样的情况,你必须使用前两个字典的键(dict1:interface和dict2:ethernet)进行迭代
a['interfaceStatuses']['Ethernet49']['linkStatus']
Basically you have dictionary of dictionary of dictionary. For this case, its simply. But if you have multiple of such cases, you have to iterate with the keys of first two dictionaries (dict1: interface and dict2: ethernet)
a['interfaceStatuses']['Ethernet49']['linkStatus']
相关问答
更多-
Python通配符搜索字符串(Python wildcard search in string)[2022-04-10]
正则表达式可能是最简单的解决这个问题的方法: import re regex = re.compile('th.s') l = ['this', 'is', 'just', 'a', 'test'] matches = [string for string in l if re.match(regex, string)] Regular expressions are probably the easiest solution to this problem: import re regex = re.c ... -
Python - 字符串搜索(Python - String search)[2022-05-02]
a = "IAMASTRIPETHA-IWANTTOIGN-RE" b = "IAMA-TRIPETHATIWA-TTOIGNORE" all(x==y or x=="-" or y=="-" for x, y in zip(a, b)) >> True a = "IAMASTRIPETHA-IWANTTOIGN-RE" b = "IAMA-TRIPETHATIWA-TTOIGNORE" all(x==y or x=="-" or y=="-" for x, y in zip(a, b)) >> Tr ... -
您可以将匹配该值的捕获组添加到您的正则表达式中: >>> int(re.search("curr_items (\d+)", out).group(1)) 0 You can add a capture group matching the value to your regex: >>> int(re.search("curr_items (\d+)", out).group(1)) 0
-
Python while - 用于嵌套循环字符串搜索用户输入(Python while - for nested loop String search on user input)[2022-08-18]
假设你在你发布的片段上面有一些像f = open('BigTextFileFullOfFruit.txt', 'r')的行:你只能在文件上迭代一次。 你必须再次调用f.seek(0)来f.seek(0)它。 Assuming you have some line like f = open('BigTextFileFullOfFruit.txt', 'r') above the snippet you posted: You can only iterate once over a file. You ha ... -
你正在调用re.search ,但你没有对返回的匹配做任何事情,除非要检查是否有。 相反,您只是打印出原始text 。 所以当然你得到整条线。 解决方案很简单:只需将re.search的结果存储在变量中,这样就可以使用它。 例如: for text in shakes: match = re.search('JOL":"(.+?).tr', text) if match: print >> needed, match.group(1) 在示例中,匹配是JOL":"EuXaq ...
-
您可以使用selenium Webdriver轻松完成此任务,在这种情况下,我们将提取Google Chrome的版本号。 以下示例代码包含解释每个步骤的注释: from selenium import webdriver # executable path should be the place where chrome driver is on the computer driver = webdriver.Chrome(executable_path= '/users/user/Downloads ...
-
$匹配字符串的结尾。 >>> import re >>> re.search("1234$","lkjlkj1234") # matches <_sre.SRE_Match object at 0x6a10578> >>> re.search("1234$","lkjlkj1234ff") # does not match >>> 所以你可能想要的表达方式是 s = re.search('([0-9a-f]{4})$',name) $ matches the end of the string. >>> ...
-
基本上你有字典词典的字典。 对于这种情况,它简单。 但是如果你有多个这样的情况,你必须使用前两个字典的键(dict1:interface和dict2:ethernet)进行迭代 a['interfaceStatuses']['Ethernet49']['linkStatus'] Basically you have dictionary of dictionary of dictionary. For this case, its simply. But if you have multiple of s ...
-
Python搜索字符串一个班轮(Python Search String One Liner)[2022-10-13]
不幸的是,在python中引入缩进的构造就像if, for其中不允许在其他语句之前。 即使在您的arg.py文件中,也请尝试以下操作: import sys; for line in sys.stdin: print line 您将发现语法也无效,导致相同的错误。 所以,要回答你的问题,你的问题不在于你在终端中运行程序,但问题在于python语法本身。 它不支持这样的语法检查相关的问题 Unfortunately, constructs that introduce indentation in pyth ... -
搜索字符串的前面以替换字符串Perl的结尾(Search the front of a string to replace the end of the string Perl)[2022-09-21]
看起来它使用固定宽度字段,所以 sub trim { $_[0] =~ s/^\s+//r =~ s/\s+\z//r } while (<>) { my $code = trim(substr($_, 2, 4)); if ($code eq "HW") { substr($_, 2, 4, " HT"); substr($_, 43, 6, " 2002"); } elsif ($code eq "OW") { substr($_, ...