Sin & Cos trouble
Blitz3D Forums/Blitz3D Beginners Area/Sin & Cos trouble
| ||
Hi All its been a while. Line 164,188,164+Sin(angle*4)*distance,188+Cos(angle*4)*distance im using the above to draw a line starting at 164,188 screen coordinates. the routine draws the line at various angles and distances. What i need to know is at which screen coordinate does the line actually end. basically i need to find the x,y pixel coordinates at which the line stops drawing. I thought i could just do: x=164+Sin(angle*4)*distance y=188+Cos(angle*4)*distance but that doesnt work. Any help would be greatly appreciated. many thanks |
| ||
I think sin(angle*4) will be converted to an integer once multiplied by distance in this example - assuming 'distance' is defined as an integer, hence the incorrect x & y try x = 164 + sin(angle*4) * float( distance ) |
| ||
It should work. The line command draws from x, y to x1, y1. So the end point coordinate is x1, y1. |
| ||
That doesnt work either just gives the same result for some reason. |
| ||
Looks like a bracketting problem try x=164+(Sin(angle*4)*distance) y=188+(Cos(angle*4)*distance) |
| ||
Still doing the same thing,heres the whole code if that helps any: It should run ive removed the graphic loading & drawing stuff. The function shotangle draws the line & uses sin & cos ;***fielder testing************* Graphics 640,480,16 SetBuffer BackBuffer() Dim field1%(2),xfield%(15,11),yfield%(15,11) Dim game%(19,1),history%(2,5),game_results(18,1) ;Global pitch_graphic = LoadImage("pitch2.png") ;Global fielder = LoadImage("fielder.png") Global rnd1 Global x,y,angle,distance ;***READ FIELD POSITIONS*** ;Gosub readfield ;***Draw pitch************* m=0 While m<10 Cls ;DrawBlock pitch_graphic ,0,78 ;** Destination in screen coordinates** Text 10,10,"X="+x Text 10,20,"y="+y For i%=1 To 11 Color 202,20,20 ;PCIRCLE xfield%(n%,i%),yfield%(n%,i%),1 ;Oval 2*xfield%(n%,i%),2*yfield%(n%,i%),10,10,1 ;DrawImage fielder,2*xfield%(8,i%),(2*yfield%(8,i%))+26,0 Next rand1(6) r=rnd1 rand1(90) shotangle(rnd1,r) Flip VWait Delay 1000 m=m+1 Wend WaitKey End ;/***random numbers**/ Function rand1(n) rnd1=Rand(0,n) End Function Function shotangle(angle%,distance%) If distance>3 distance=distance*30 If distance<4 Then distance=distance*25 ;;limit distance;; If angle%>-1 And angle%<12 And distance%>105 Then distance%=105 If angle%>11 And angle%<18 And distance%>90 Then distance%=90 If angle%>17 And angle%<26 And distance%>82 Then distance%=82 If angle%>25 And angle%<33 And distance%>68 Then distance%=68 If angle%>32 And angle%<59 And distance%>60 Then distance%=60 If angle%>58 And angle%<63 And distance%>70 Then distance%=70 If angle%>62 And angle%<79 And distance%>92 Then distance%=92 If angle%>78 And angle%<91 And distance%>105 Then distance%=105 distance=distance*2 Color 255,255,255 Line 164,188,164+Sin(angle*4)*distance,188+Cos(angle*4)*distance Color 200,200,200 x=164+(Sin(angle*4)*distance) y=188+(Cos(angle*4)*distance) End Function ;**FIELD POSITIONS** .readfield Restore positions For a=1 To 15 For i=1 To 11 Read xfield(a,i),yfield(a,i) Next Next .positions ;''normal 1'' Data 74,69,66,112,26,72,53,34,119,72,120,111 Data 47,144,102,142,57,60,6,113,100,42 ;''normal 2'' Data 74,69,66,112,24,56,58,58,109,99,7,103 Data 126,69,90,35,116,140,35,126,77,159 ;''normal 3'' Data 74,69,66,112,43,51,22,81,26,123,46,150 Data 93,36,101,93,128,59,121,144,86,139 ;''normal 4'' Data 74,69,66,112,59,64,98,45,128,81,120,128,85,157 Data 101,96,38,150,45,106,23,75 ;''normal 5'' Data 74,69,66,112,63,60,128,74,98,37,134,121,99,149 Data 18,119,53,97,51,147,110,99 ;''attack 1'' Data 74,69,66,112,63,61,53,57,55,89,99,100 Data 97,45,119,76,45,131,111,148,31,73 ;''attack 2'' Data 74,69,66,112,47,95,106,96,63,61,54,58 Data 121,71,27,71,43,135,102,134,94,52 ;''attack 3'' Data 74,69,66,112,64,63,56,58,99,78,115,103 Data 101,129,37,74,57,91,24,113,51,139 ;''attack 4'' Data 74,69,66,112,63,61,58,83,88,82,104,60 Data 26,70,25,103,44,133,119,104,96,144 ;''attack 5'' Data 74,69,66,112,62,62,40,79,49,103,100,77 Data 88,97,129,121,51,138,100,142,98,52 ;''defend 1'' Data 74,69,66,112,18,79,136,76,48,41,97,37 Data 14,114,30,139,126,110,100,124,64,153 ;''defend 2'' Data 74,69,66,112,105,43,121,74,138,99,118,115 Data 108,152,56,138,31,118,35,74,50,48 ;''defend 3'' Data 74,69,66,112,53,43,35,66,13,93,24,129 Data 55,147,118,52,129,84,102,104,102,154 ;''defend 4'' Data 74,69,66,112,10,77,124,75,52,33,106,40 Data 30,105,33,143,69,162,123,107,100,136 ;''defend 5'' Data 74,69,66,112,101,36,130,73,121,101,124,141 Data 88,153,38,150,16,114,23,73,51,53 Return |
| ||
Can't really be arsed to look at your code, so here's something that might help...Graphics 640,480 Repeat Cls x=Rand(0,639) y=Rand(0,480) For n=1 To 50 angle = Rand(0,359) distance = Rand(0,200) Line x,y,x+(Sin(angle)*distance),y+(Cos(angle)*distance) Delay 50 Next Until KeyHit(1) |
| ||
If you do Sin(45)+Cos(45) it should be equal to 1 or almost (few tenths off because of floats inprecision). So if you find the distance between x,y and x1 and y1, just multiply sin and cos by that. In theory it should work, or at least for me. |
| ||
Thanks for trying rob,but your codes the same as mine i still cant find the destination of the line using: x1=x+(Sin(angle)*distance) y1=y+(Cos(angle)*distance) There must be an easy way of doing this?. |
| ||
Pazza, It's not very clear from your code what you're trying to acheive here. If you could explain in more detail I'm sure there'll be a more elegant way of doing this. You're doing alot of messing about with 'distance', probably unecessarily - which may be you're problem. Happy to help if you give more details :) |
| ||
I really don't understand what you're trying to achieve. x1=x+(Sin(angle)*distance) y1=y+(Cos(angle)*distance) x/y is the beginning of the line, x1/y1 is the end of the line. |
| ||
all i want is the pixel coordinates for the end of the line,the line starts at the same position so i know the beggining i just cant get the end screen position. Heres a simpler example,x1 & y1 variables should give the end point of the line in pixel coordinates,but if you run it they dont give the proper screen coordinates. Graphics 640,480,16 SetBuffer BackBuffer() distance=90 ;***Draw pitch************* m=0 While m<10 Cls Text 10,10,"X1="+x1 Text 10,20,"y1="+y1 Color 202,20,20 angle=Rand(0,90) Line 164,188,164+(Sin(angle*4)*distance),188+(Cos(angle*4)*distance) x1=164+(Sin(angle*4)*distance) y1=188+(Cos(angle*4)*distance) Flip VWait Delay 1000 m=m+1 Wend end |
| ||
Doh sorry guys ive worked it out my mistake. Thats what you get for programming whilst drinking beer last night and a megga hangover today. |