转角函数python 三角函数的旋转角度数

如何用python turtle 画n阶希尔伯特曲线

可以啊 ,你就想怎么用第一阶的图像把第二阶的构造出来,主要是输入和控制转角,,,,因为这是递归函数,所以你只要把第二阶画,出来即可,,

成都创新互联制作网站网页找三站合一网站制作公司,专注于网页设计,成都网站设计、成都网站制作、外贸网站建设,网站设计,企业网站搭建,网站开发,建网站业务,680元做网站,已为近1000家服务,成都创新互联网站建设将一如既往的为我们的客户提供最优质的网站建设、网络营销推广服务!

已知三点坐标,如何求夹角?

设两直线斜率分别为k1,k2

夹角θ=arctan|(k1-k2)/(1+k1k2)|

这个公式可能会提供你一些想法

自己试试吧

现在有点忙

没法给你写代码。

如何用python画一个Koch snowflake

可以这样做:

生成转角序列,然后转换为像素坐标。最后用python的PIL模块画图,保存为"koch.bmp"并显示图形。概念性代码:

#!/usr/bin/env python

#coding:utf-8

from PIL import Image, ImageDraw

from math import sin, cos, pi

def genRaList(raListIn, n):

raListOut = raListIn

for i in range(n):

raListOut = []

for ra in raListIn:

raListOut.extend([ra, -60, 120, -60])

raListIn = raListOut

return raListOut

def raToPoints(xy, l, raList, n):

degreeToRadian = pi/180

angleDegree = 0

r = l*(3**(-n))

x,y = xy

pt = [(x,y)]

for ra in raList:

angleDegree += ra

angleRadian = angleDegree*degreeToRadian

x += r*cos(angleRadian)

y += -r*sin(angleRadian)

pt.append((x,y))

return pt

def drawKoch(xy, l, size, raList0, n):

raList = genRaList(raList0, n)

points = raToPoints(xy, l, raList, n)

im = Image.new('1', size, 'white')

draw = ImageDraw.Draw(im)

draw.polygon(points, fill=None, outline='black')

im.save('koch.bmp')

im.show()

if __name__ == '__main__':

raList0 = [240, 120, 120]

drawKoch((207, 34), 300, (415, 415), raList0, 5)

飞思卡尔程序

我这只有舵机,摄像头,和主程序,但是只是框架,要等调试。可以参考哈

#ifndef _WATCH_H_ //头文件保护

#define _WATCH_H_

#ifndef ULONG

#define ULONG unsigned long

#define UCHAR unsigned char

#define UINT unsigned int

#endif

#define ROAD_MAX 10

////////////////代设值

#define MP0 //捕捉象素引脚

#define HREF //捕捉行中断引脚

//////////////////

static unsigned char Colors[10][48]; //象素数组

static unsigned char JudgeMax; //最大象素

static unsigned char JudgeMin; //最小象素

unsigned char Over; //中断函数处理完成

//使用数组后要清零

struct ROADS

{

unsigned char LeftRight; //1 left, 0 right 2 表示没检测到黑线 4表示起始线

unsigned int road; //转向率,0表示直线或则没检测到黑线

}Roads[10]; //没检测到黑线一般为前方有大于90度的弯,

//行驶一会后数组会更新,不会因此丢掉跑道

unsigned long RoadLength; //mm单位

void GetRoads(); //取象素

void ProcessRoads(); //取转向率

void delay(UINT); //延时函数

void ColorToRoads(UINT); //2值化与赋值

#endif //_WATCH_H_

以下为实现文件:

//watch.c

#include hidef.h /* common defines and macros */

#include MC9S12XS128.h /* derivative information */

#include "watch.h"

#pragma CODE_SEG NON_BANKED

#pragma TRAP_PROC

void GetRoads() //场中断服务函数

{

UINT i;

UINT Href= 0;

delay(5);

while(HREF)

{

if(Href= 270) //点读取完毕,退出循环

break;

if((270-Href)%21!= 0 || Href 60) //取60-270行中平均间隔的行数,共30行

{

Href++;

while(HREF); //等待此行扫描结束

while(!HREF); //等待新行开始扫描

continue;

}

delay(...); //根据调式结果具体设置

for(i= 0; i 48; i++)

{

Colors[((270-Href)%21)][i]= MP0; //读取引脚数据,后来会根据串口还是并口读取做相应修改

if(MP0 judgeMax) //取最大最小值

JudgeMax= MP0;

if(MP0 JudgeMin)

JudgeMin= MP0;

delay(...); //具体设置

}

Href++;

while(HREF);

while(!HREF); //等待行中断结束

}

Over= 1; //中断函数处理完成

}

#pragma CODE_SEG DEFAULT

void ColorToRoads(UINT Href)

{

UINT i;

UINT temp; //用于起始线判断

UINT temp2;

UINT temp3;

UCHAR Judge; //平均厥值

UINT j= 0;

UINT m_nCount= 0;

Judge= (JudgeMax+ JydgeMin)/2;

Roads[Href].road= 0;

Roads[Href].LeftRight= 0;

for(i= 0; i48; i+= 3)

{

if(Colors[i]= Judge||

Colors[i+ 1]= Judge||

Colors[i+ 2]= Judge)

{

Roads[Href].road= (0x8000 (UCHAR)j); //相应位赋值为1

m_nCount++;

}

j++;

}

if(Roads[Href].road== 0x00)

{

Roads[Href].LeftRight= 0x02; //没有捕捉到黑线设置2

}

if(m_nCount 1) //若黑点数大于1,从左向右数出连续的点中的中间点

{

j= 0;

i= 0;

while(!(Roads[Href].road (0x8000(UCHAR)i)))

{

i++;

}

j= i;

while((Roads[Href].road (0x8000(UCHAR)i))

{

i++;

if(i 15)

break;

}

if(i 15) //起始线判断

{

temp= i;

while(!(Roads[Href].road (0x8000(UCHAR)temp)))

{

temp++;

temp2= temp;

}

if(temp 15)

{

while((Roads[Href].road (0x8000(UCHAR)temp))

{

temp++;

temp3= temp;

if(temp 15)

break;

}

if(temp 15)

{

while(!(Roads[Href].road (0x8000(UCHAR)temp)))

{

temp++;

}

if(temp 15)

{

Roads[Href].LeftRight= 0x04;

Roads[Href].road= 0x8000(UCHAR)((temp2+ temp3)/2);

return;

}

}

}

}

Roads[Href].road= 0x8000(UCHAR)((i+j)/2);

}

}

void ProcessRoads() //路径处理函数,在主函数中调用

{

int i;

for (i= 0;i 10; i++)

{

ColorToRoads(i);

}

}

void delay(UINT m) //延时函数根据调试结果相应做改动

{

UINT i;

UINT j;

for(i= 0; i m; i++)

{

for(j= 0; j 200; j++)

}

}

再下面就是主函数实现文件:

#include hidef.h /* common defines and macros */

#include MC9S12XS128.h /* derivative information */

#pragma LINK_INFO DERIVATIVE "mc9s12xs128"

#include "watch.h" //摄像头

#include "TurnAround.h" //舵机

//#include "..." //直流电机

//#include "..." //测速模块

#ifndef TRUE

#define TRUE 1

#define FLASE 0

#endif

#define ULONG unsigned long

#define UCHAR unsigned char

#define UINT unsigned int

#define LITTLE

#define LARGE

//////////////////////全局变量

UCHAR m_nCount; //圈数计算

UCHAR m_nCount2;

////////////////////////////////函数定义

void init(); //初始化函数 待修改

UCHAR CheckRoad(UCHAR*,UCHAR*); //第1参数返回第几个元素开始

//第2个参数返回第几个元素结束

//0 直线, 0x11有小弯道, 0x21大弯道, 0x31终点

void CarRun(UCHAR, UCHAR, UCHAR); //小车行驶函数

#pragma CODE_SEG NON_BANKED

#pragma TRAP_PROC

void Int_TimerOverFlow(void) //32MHz TCNT 50000--200ms

{

static int m= 0;

if(m== 15) //3秒

{

m_nCount2= 0;

TSCR1_TEN= 0; //关定时器

TFLG2_TOF = 1;

}

m++;

}

#pragma CODE_SEG DEFAULT

void TimerInit()

{

TIOS= 0x00;

TSCR2_PR= 7; //默认情况下是32MHz bus clock,所以分频后主时钟为 0.25MHz

TSCR2_TOI= 1; //OverFlow方式

TCNT= 65535- 50000;

TSCR1_TEN= 1; //开定时器

}

////////////////////////////////

void main() //主程序

{

UCHAR ret;

UCHAR number1= 0;

UCAHR number2= 0;

init();

while(1)

{

while(over); //等待扫描完毕

over= 0;

IRQCR_IRQEN= 0; //关外部中断IRQ

ProcessRoads(); //interface of watch.h

ret= CheckRoad(number1, number2); //检测路径

if(ret== 0x31)

break;

CarRun(number1, number2, ret);

}

PWME_PWME1= 0; //关闭PWM

while(1);

}

void init()

{

IRQCR_IRQE= 1; //下降沿触发IRQ

PE1= 1;

EnableInterrupts; //开总中断

INTCR_IRQEN= 1;

PWMInit(); //舵机初始化

Speed(MAX);

}

UCHAR CheckRoad(UCHAR* number1, UCHAR number2)

{

int i;

int sub= 0;

int psub= 0;

int temp;

bool flag;

UCHAR ret;

i= 0;

m_nCount2++;

while(!Roads[i].road)

{

i++;

}

*number1= i;

if(Roads[i].road= Roads[i+ 1].road)

{

flag= TRUE;

psub= Roads[0].road- Roads[1].road;

}

else

{

flag= FALSE;

psub= Roads[1].road- Roads[0].road;

}

for(; i 9; i++)

{

if(Roads[i].LeftRight== 0x04)

{

if(m_nCount2== 0)

{

m_nCount2= 1;

m_nCount++;

TimerInit();

}

}

if(m_nCount== 3)

{

Speed(0);

ret= 0x31;

goto _RET;

}

if(Roads[i+ 1].road== 0)

{

break;

}

if(flag)

{

sub= Roads[i].road- Roads[i+ 1].road;

}

else

{

sub= Roads[i+ 1].road- Roads[i].road;

}

sub= (UCHAR)i;

if(sub= psub) //取差值

temp= sub- psub;

else

temp= psub- sub;

if(temp LARGE) //大弯道

{

ret= 0x21;

while(Roads[i].road!= 0)

{

i++;

*number2= i;

if(i 9)

break;

}

goto _RET;

}

if(temp LITTLE) //小弯道

{

ret= 0x11;

while(Roads[i].road!= 0)

{

i++;

*number2= i;

if(i 9)

break;

}

goto _RET;

}

*number2= i;

psub= sub;

}

_RET:

return ret;

}

void CarRun(UCHAR number1, UCHAR number2, UCHAR ret)

{

int end= Roads[number2].road;

Turn(number2- number1, end- 0x100, Roads[number2].LeftRight, ret); //舵机函数

IRQCR_IRQEN= 1; //开IRQ

}

下面为舵机头文件:

//TurnAround.h

#ifndef _TURNAROUND_H_ //头文件保护

#define _TURNAROUND_H_

void Turn(unsigned char, unsigned int, unsigned char, unsigned char); //转角函数

void PWMInit(); //优先初始化

#endif

再以下为实现文件:(表内容待实验后求得)

//TurnAround.c

#include hidef.h /* common defines and macros */

#include MC9S12XS128.h /* derivative information */

#pragma LINK_INFO DERIVATIVE "mc9s12xs128"

#include "TurnAround.h"

#include "..." //直流电机

#ifndef ULONG

#define ULONG unsigned long

#define UCHAR unsigned char

#define UINT unsigned int

#endif

/////////////////////////转角定义

#define RIGHT60 1083

#define RIGHT45 1000

#define RIGHT30 917

#define RIGHT15 833

#define RIGHT5 778

#define MIDDLE 750

#define LEFT5 722

#define LEFT15 667

#define LEFT30 583

#define LEFT45 500

#define LEFT60 417

/////////////////////////速度定义

#define SPEED0 //直线速度

#define SPEED5

#define SPEED15

#define SPEED30

#define SPEED45

#define SPEED60

//PWM查询表 7*10*2

static UINT PWMTable[]=

{

//Left

LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60,

LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60,

LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60,

LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60,

LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60,

LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60,

LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60, LEFT60,

//Right

RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60,

RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60,

RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60,

RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60,

RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60,

RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60,

RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60, RIGHT60

};

//延时查询表 6*6

static UNIT DelayTable[]=

{

0,0,0,0,0,0,

0,0,0,0,0,0,

0,0,0,0,0,0,

0,0,0,0,0,0,

0,0,0,0,0,0,

0,0,0,0,0,0

}

void PWMInit() //32HHz

{

PWMCTL_CON01= 1; //0, 1合为16bit

PWMCAE_CAE1= 1; //Center mode

PWMCLK_PLCK1= 0; //Clock A

PWMPRCLK_PCKA= 5; //Clock A= 32MHz/32= 1MHz

PWMPOL_PPOL1= 0; //开始为低电压

PWMPER0,1= 10000; //50Hz PWM输出

PWMDTY0,1= MIDDLE; //(10000- 750)* 100%= 92。5%占空比,1.5ms高电压时间

PWME_PWME1= 1; //enable

}

static void delay(int ms)

{

int ii,jj;

if (ms1) ms=1;

for(ii=0;iims;ii++)

for(jj=0;jj2770;jj++); //32MHz--1ms

}

static void SetPWM(UINT m_nPWM) //PWM设置

{

PWMDTY0,1= m_nPWM;

}

//number 9种情况,sub 7种情况,LeftRight 2中情况

//共128种情况,采用模糊控制暂时归纳为11种情况

//11种情况将填入表中提供查询,11中情况分别为:

//左右5, 15, 30, 45, 60度

//和正中间0度

static UINT GetPWM(UCHAR number, UINT sub, UCHAR LeftRight)

{

switch(sub) //通过sub算出索引值

{

case 0x8000- 0x0100:

case 0x0100- 0x0001;

case 0x0100- 0x0002;

sub= 0;

break;

case 0x4000- 0x0100:

case 0x0100- 0x0004:

sub= 1;

break;

case 0x2000- 0x0100:

case 0x0100- 0x0008:

sub= 2;

break;

case 0x1000- 0x0100:

case 0x0100- 0x0010:

sub= 3;

break;

case 0x0800- 0x0100:

case 0x0100- 0x0020:

sub= 4;

break;

case 0x0400- 0x0100:

case 0x0100- 0x0040:

sub= 5;

break;

case 0x0200- 0x0100:

case 0x0100- 0x0100:

case 0x0100- 0x0080:

sub= 6;

break;

}

return PWMTable[number+ sub* 10+ LeftRight* 70];

}

//通过m_nPWM来改变速度,并返回改变的值

static UINT ChangeSpeed(UINT m_nPWM)

{

UINT m_nSpeed;

switch(m_nPWM) //根据m_nPWM 调节速度

{

case LEFT60:

Speed(SPEED60);

m_nSpeed= SPEED60;

break;

case LEFT45:

Speed(SPEED45);

m_nSpeed= SPEED45;

break;

case LEFT30:

Speed(SPEED30);

m_nSpeed= SPEED30;

break;

case LEFT15:

Speed(SPEED15);

m_nSpeed= SPEED15;

break;

case LEFT5:

Speed(SPEED5);

m_nSpeed= SPEED5;

break;

case MIDDLE:

Speed(SPEED0);

m_nSpeed= SPEED0;

break;

case RIGHT60:

Speed(SPEED60);

m_nSpeed= SPEED60;

break;

case RIGHT45:

Speed(SPEED45);

m_nSpeed= SPEED45;

break;

case RIGHT30:

Speed(SPEED30);

m_nSpeed= SPEED30;

break;

case RIGHT15:

Speed(SPEED15);

m_nSpeed= SPEED15;

break;

case RIGHT5:

Speed(SPEED5);

m_nSpeed= SPEED5;

break;

}

return m_nSpeed;

}

//获得查表时的索引值

UINT GetIndex(UINT m_nSpeed)

{

if(m_nSpeed= SPEED60)

{

m_nSpeed= 0;

}

else if(m_nSpeed= SPEED45)

{

m_nSpeed= 1;

}

else if(m_nSpeed= SPEED30)

{

m_nSpeed= 2;

}

else if(m_nSpeed= SPEED15)

{

m_nSpeed= 3;

}

else if(m_nSpeed= SPEED5)

{

m_nSpeed= 4;

}

else

{

m_nSpeed= 5;

}

return m_nSpeed;

}

//m_nSpeed2为欲设值

//m_nSpeed为当前速度

UINT GetDelay(UINT m_nSpeed, UINT m_nSpeed2)

{

m_nSpeed= GetIndex(m_nSpeed);

m_nSpeed2= GetIndex(m_nSpeed2);

return DelayTable[m_nSpeed* 6+ m_nSpeed2];

}

void Turn(UCHAR number, UINT sub, UCHAR LeftRight, UCHAR ret)//ret not be used now

{

UINT m_nPWM;

UINT m_nSpeed;

UINT m_nSpeed2;

UINT m_nDelay; //延时参数

m_nPWM= GetPWM(number, sub, LeftRight);

m_nSpeed= GetSpeed() //测速模块

m_nSpeed2= ChangeSpeed(m_nPWM);

if(m_nSpeed2 m_nSpeed)

m_nSpeed= m_nSpeed2- m_nSpeed;

else

m_nSpeed= m_nSpeed- m_nSpeed2;

SetPWM(m_nPWM); //转角

m_nDelay= GetDelay(m_nSpeed, m_nSpeed2);

delay(m_nDelay); //根据速度和角度延时

SetPWM(MIDDLE); //舵机摆正

}

最后说哈,程序只差调试就可以,筐架就是这。我是湖北赛区的,7月就要比赛了,他们车还没做好啊。

利用python绘制太阳花

识别出图形中的基本形状,以基本形状为单位绘制出最终图形。绘制所示图形,中间是半径为120的圆, 四周是边长为80的12个菱形。

使用turtle绘制如图中所示的图形。

1) 背景为白色,中间圆为红色轮廓线,不填充;

2)图中菱形的长对角线延长线经过圆心(如图中虚线所示,虚线不用画。

3)菱形为黑色轮廓线、黄色填充,其中锐角为60度;

4)绘图过程中隐藏画笔,能清楚地看到图形绘制过程。

首先我们需要导入turtle海龟函数库,在海龟函数库中,我们有着许多关于图形绘制的方式和方法。

我们首先利用circle方法绘制出一个圆形,其次再利用pencolor方法进行画笔颜色的设置,颜色设置从成红色。

在利用每次循环控制菱形的绘制,菱形的边长是一样长的,菱形每次的转角是不一样的。

画完一次的菱形后,因为菱形需要重复12次,所以我们利用for循环,来控制次数,画出多个相同大小不同位置的菱形。

最终结果如下所示。


本文名称:转角函数python 三角函数的旋转角度数
文章地址:http://csdahua.cn/article/doojihj.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流