热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

用python生成随机矩形_如何在我的海龟圆和矩形内生成随机点?

在矩形内部生成随机点,很简单。您只需生成一个随机的x坐标,范围从原点位置(-75,在您的例子中),直到它的结束,

在矩形内部生成随机点,很简单。您只需生成一个随机的x坐标,范围从原点位置(-75,在您的例子中),直到它的结束,这将是原点+宽度(-75+100)。

然后,对y坐标做同样的处理。然后,移动到生成的位置并绘制一个点。

我的代码:# draw random dots inside of rectangle

# @param origin: is a touple, containing `x` and `y` coordinates

# @param number_of_dots: int, number of dots

# @param size: is a touple, containing `width` and `height` of rectangle

def draw_random_dots_in_rectangle(origin, number_of_dots, size=RECTANGLE_SIZE):

# loops number_of_dots times

for _ in range(number_of_dots):

# generate a random position inside of given rectangle

# using min/max, because of possible negative coordinates

# weakness - does also place dots on the edges of the rectangle

rand_x = randint(min(origin[0], origin[0] + size[0]), max(origin[0], origin[0] + size[0]))

rand_y = randint(min(origin[1], origin[1] + size[1]), max(origin[1], origin[1] + size[1]))

# moves to the random position

move_turtle_to((rand_x, rand_y))

# creates a dot

t.dot(DOT_DIAMETER)

但是,对circle做同样的事情是不可能的。它要复杂得多,并且需要analytic geometry的知识。在您的例子中,您需要equation of circles。用它你可以计算,如果生成的位置是,或不在给定圆内。在

我的代码:

^{pr2}$

从本质上讲,这一过程与以前相同,但有等式检验。

我希望这至少能帮上一点忙。我自己也曾多次努力弄清楚如何在计算机科学中做某些事情,而且很多时候我发现,解析几何是答案。所以我强烈建议你至少检查一下。

我的孔代码:#!/usr/bin/env python3

import turtle

from random import randint

RECTANGLE_SIZE = 60, 80

CIRCLE_RADIOUS = 10

DOT_DIAMETER = 3

t = turtle.Turtle() # turtle object

t.speed(0) # set the fastest drawing speed

# move turtle to position without drawing

# @param: position is a touple containing `x` and `y` coordinates

def move_turtle_to(position):

t.up() # equivalent to .penuo()

t.goto(position[0], position[1])

t.down() # equivalent to .pendown()

# draws a rectangle from given origin with given size

# @param origin: is a touple, containing `x` and `y` coordinates

# @param size: is a touple, containing `width` and `height` of rectangle

def draw_rectangle(origin, size=RECTANGLE_SIZE):

# movese to the origin

move_turtle_to(origin)

# simple way of drawing a rectangle

for i in range(4):

t.fd(size[i % 2])

t.left(90)

# draws a circle from given origin with given radious

# @param origin: is a touple, containing `x` and `y` coordinates

# @param radious: int, radious of circle

def draw_circle(origin, radius=CIRCLE_RADIOUS):

# moves to the origin

move_turtle_to(origin)

# draws the circle

t.circle(radius)

# Now to what you asked

# draw random dots inside of rectangle

# @param origin: is a touple, containing `x` and `y` coordinates

# @param number_of_dots: int, number of dots

# @param size: is a touple, containing `width` and `height` of rectangle

def draw_random_dots_in_rectangle(origin, number_of_dots, size=RECTANGLE_SIZE):

# loops number_of_dots times

for _ in range(number_of_dots):

# generate a random position inside of given rectangle

# using min/max, because of possible negative coordinates

# weakness - does also place dots on the edges of the rectangle

rand_x = randint(min(origin[0], origin[0] + size[0]), max(origin[0], origin[0] + size[0]))

rand_y = randint(min(origin[1], origin[1] + size[1]), max(origin[1], origin[1] + size[1]))

# moves to the random position

move_turtle_to((rand_x, rand_y))

# creates a dot

t.dot(DOT_DIAMETER)

# draw random dot inside of circle

# @param origin: is a touple, containing `x` and `y` coordinates

# @param number_of_dots: int, number of dots

# @param radious: int, radious of circle

def draw_random_dots_in_circle(origin, number_of_dots, radius=CIRCLE_RADIOUS):

# loops number_of_dots times

for _ in range(number_of_dots):

# loops until finds position inside of the circle

while True:

# generates random x position

# subtracting radious and adding double of radious to simulate bounds of square

# which would be large enought to fit the circle

rand_x = randint(min(origin[0] - radius, origin[0] + radius * 2),

max(origin[0] - radius, origin[0] + radius * 2))

# generated random y position

# adding double of radious to sumulate bounds of square

# which would be large enought to fit the circle

rand_y = randint(min(origin[1], origin[1] + radius * 2),

max(origin[1], origin[1] + radius * 2))

# test if the generated position is in the radious

if (origin[0] - rand_x) ** 2 + (origin[1] + radius - rand_y) ** 2

# if it is, move to the position

move_turtle_to((rand_x, rand_y))

# draw dot

t.dot(DOT_DIAMETER)

# break out from the infinite loops

break

# example code

draw_rectangle((0, 0))

draw_random_dots_in_rectangle((0, 0), 50)

draw_circle((-20, -20))

draw_random_dots_in_circle((-20, -20), 20)

input()



推荐阅读
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 3.223.28周学习总结中的贪心作业收获及困惑
    本文是对3.223.28周学习总结中的贪心作业进行总结,作者在解题过程中参考了他人的代码,但前提是要先理解题目并有解题思路。作者分享了自己在贪心作业中的收获,同时提到了一道让他困惑的题目,即input details部分引发的疑惑。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • IB 物理真题解析:比潜热、理想气体的应用
    本文是对2017年IB物理试卷paper 2中一道涉及比潜热、理想气体和功率的大题进行解析。题目涉及液氧蒸发成氧气的过程,讲解了液氧和氧气分子的结构以及蒸发后分子之间的作用力变化。同时,文章也给出了解题技巧,建议根据得分点的数量来合理分配答题时间。最后,文章提供了答案解析,标注了每个得分点的位置。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 推荐系统遇上深度学习(十七)详解推荐系统中的常用评测指标
    原创:石晓文小小挖掘机2018-06-18笔者是一个痴迷于挖掘数据中的价值的学习人,希望在平日的工作学习中,挖掘数据的价值, ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 也就是|小窗_卷积的特征提取与参数计算
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了卷积的特征提取与参数计算相关的知识,希望对你有一定的参考价值。Dense和Conv2D根本区别在于,Den ... [详细]
  • FeatureRequestIsyourfeaturerequestrelatedtoaproblem?Please ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • Python教学练习二Python1-12练习二一、判断季节用户输入月份,判断这个月是哪个季节?3,4,5月----春 ... [详细]
  • 在本教程中,我们将看到如何使用FLASK制作第一个用于机器学习模型的RESTAPI。我们将从创建机器学习模型开始。然后,我们将看到使用Flask创建AP ... [详细]
author-avatar
豪哥帅366
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有