Shop OBEX P1 Docs P2 Docs Learn Events
Cartesian Coordinate Circle....problems! — Parallax Forums

Cartesian Coordinate Circle....problems!

James LongJames Long Posts: 1,181
edited 2006-12-30 08:08 in Propeller 1
Help I'm looking for a circle but I'm getting a elliptical.

math = float32full

tempx := math.fround(math.fadd(math.Fmul(math.cos(math.ffloat(angle_altitude)),35.0),240.0))
tempy := math.fround(math.fadd(math.Fmul(math.cos(math.ffloat(angle_altitude)),35.0),78.0))

this draws lines from the center out.....hopefully in a circle not an elliptical

Thanks,

James L

Comments

  • asterickasterick Posts: 158
    edited 2006-12-29 18:12
    Are you entirely sure that it's the MATH that is elliptical?

    Traditionally, most pictures produced on a television do not have square pixels. You may need to adjust your code to accommodate the skew.
  • acantostegaacantostega Posts: 105
    edited 2006-12-29 19:08
    James Long said...
    Help I'm looking for a circle but I'm getting a elliptical.
    math = float32full
    tempx := math.fround(math.fadd(math.Fmul(math.cos(math.ffloat(angle_altitude)),35.0),240.0))
    tempy := math.fround(math.fadd(math.Fmul(math.cos(math.ffloat(angle_altitude)),35.0),78.0))
    Thanks,
    James L

    Mmm, wouldn't the tempy be r*sin(angle) (not cos())? That is,

    tempy := math.fround(math.fadd(math.Fmul(math.sin(math.ffloat(angle_altitude)),35.0),78.0))
    
    



    I don't know if you're after efficiency, but if you are, there's some routines to draw circles using only integers. Look up Bresenham's raster algorithm for circles (not lines). Raster ellipses are somewhat more involved. See www.cs.dartmouth.edu/~doug/155.ps.gz for more information on the subject you'll ever need. There's a C routine in there.

    Edit: Sorry, I thought you wanted to draw the outline of a circle, not a bunch of radially disposed lines. Then you can use Bresenham's line algorithm.

    Post Edited (acantostega) : 12/29/2006 7:25:19 PM GMT
  • James LongJames Long Posts: 1,181
    edited 2006-12-29 19:29
    Asterick said...
    Are you entirely sure that it's the MATH that is elliptical?

    Traditionally, most pictures produced on a television do not have square pixels. You may need to adjust your code to accommodate the skew.
    Well.....the elliptical is at a 45 degree angle....so it is not the pixel difference.

    acantostega
    I'm not sure what to do....it is really being a pain. I think it is the rounding of the floating point number.

    The only way I can use the display that I've bought is to draw lines from the center out. I'm sure I could do it anouther way....but it would be a major reprogramming in C....which I'm not familiar with.

    James L
  • asterickasterick Posts: 158
    edited 2006-12-29 19:55
    This was a quick and dirty, I'm not 100% sure that it works, but it's close enough

    pub PlotCircle( X, Y, Radius ) | Sum, XOff, YOff
      XOff := 0
      YOff := Radius
      Sum := 3 - (Radius << 1)
      
      repeat while XOff <= YOff
        Plot( X + XOff, Y + YOff )
        Plot( X - XOff, Y + YOff )
        Plot( X + XOff, Y - YOff )
        Plot( X - XOff, Y - YOff )
        Plot( Y + XOff, X + YOff )
        Plot( Y - XOff, X + YOff )
        Plot( Y + XOff, X - YOff )
        Plot( Y - XOff, X - YOff )
    
        if s < 0
          s += (XOff << 2) + 6
        else
          s += ( ( XOff - YOff ) << 2 ) + 10
          YOff--
        XOff++
    
    



    That's a quick and dirty circle algorithm. It will at least help you avoid using floating point.
  • James LongJames Long Posts: 1,181
    edited 2006-12-29 20:24
    Will that only give me an outline....if so...I can't use it.

    Maybe someone will step in here and take a look.

    This is for a LCD screen.....

    I have included the spin.

    I probably have a huge amount of mistakes. Just be kind.

    This iteration gives me a saturn looking circle(it's not completely filled in).· The circle is there...but still has an elliptical "rings" type effect also.



    James L


    Post Edited (James Long) : 12/29/2006 8:33:31 PM GMT
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2006-12-29 23:22
    Its an ellipse not an elliptical though it is elliptical in shape!

    change one cos to a sin, if you think about where cos and sin actually come from it will make sense. For a line of unit length at angle theta then:

    y = sin(theta)
    x = cos(theta)

    My appologies if I'm not reading your code right, I don't know the library and I have only about 1min to write a response and this windows ME computer won't run the IDE.

    Graham
  • James LongJames Long Posts: 1,181
    edited 2006-12-29 23:28
    Ok......I'll take that....I may have my terminology wrong....which is very common.
    Edit:· Nope....I forgot I'm dealing with angles not triangles. The calculations figure the x coordinate for a certain angle...then calculate the y coordinate of the same angle. Therefore I can plot the line.

    I'm getting closer...but still a long ways away.

    James L

    Post Edited (James Long) : 12/29/2006 11:56:47 PM GMT
  • acantostegaacantostega Posts: 105
    edited 2006-12-30 05:04
    Hmm, looking at the code I don't exactly get how the display works. If (as seggested above) you change the cos() for the sin() in the tempy assignment, the tempx and tempy should acquire the values of a circle of radius 35.0 and centered at (240.0, 78.0), as the counter progresses, right? (By the way, is there some special reason you're incrementing the altitude variable in another cog? It seems to make more sense to do it in the main loop).
    By the way, you are assigning 1 to 360 to the angle variable. Are you sure the sin() and cos() functions take the angle in degrees? It would seem to me that they should take it in radians, like in C's math.h. If that were the case I think a loop like
    CON 
       2PI = 6.28
    '' later
    repeat angle_altitude from 0 to 2PI step 0.1
       tempx := math.fround(math.fadd(math.Fmul(math.cos(math.ffloat(angle_altitude)),35.0),240.0))
       tempy := math.fround(math.fadd(math.Fmul(math.sin(math.ffloat(angle_altitude)),35.0),78.0))
    
    


    would make more sense.
    Is the display limited to making straght lines?

    Post Edited (acantostega) : 12/30/2006 5:09:16 AM GMT
  • James LongJames Long Posts: 1,181
    edited 2006-12-30 08:08
    Acantostega,

    The reason....I'm using the display as a gauge....I don't want a needle...it would be to small....so I was filling the circle in as needed to represent the amount of "whatever"

    I was incrementing using a dummy counter to simulate a sensor. The Propeller will have other cogs running getting sensor information.....so that is why the extra cog. Yea....I know that is probably overkill.....but later when I get this working (which I have already) I won't have to rewrite the whole Object.

    The only way I know of filling a circle partly with this LCD is line by line.....but I found a work around.

    But thanks to all who helped.

    James Long
Sign In or Register to comment.