Question 'bout lines (previously a question 'bout setContext())

I was trying to sprite an image onto an image, but the result came out completely blank. I don’t know if I’m doing it wrong or what. Everything else was working perfectly, drawing onto the image correctly, but when I tried drawing another image onto the image, no such luck.

could you post the code? Write 3 ~ before and afte.

I know how to format it, but thanks :stuck_out_tongue:
I’m working on getting the code posted, but it’s far too long for the forum and github isn’t working right now (for me, at least)
All it’s doing though, basically, is drawing some lines onto an image, then drawing that image onto an image(at least, that’s what it SHOULD be doing)

usually the problem is soveld with resetMatrix()

The problem was the values for width and height changing when i changed the context, had to adjust for different ratios. My bad, but thanks for the help :smiley:
EDIT: also, I only solved the issue due to me mistakenly erasing a value and it suddenly reappearing, so this wasn’t a case of “problem I could fix on my own but decided to waste your time with it”)

Question: if I make a rectangular image, then I draw things onto it, should it stretch whatever is drawn onto it? That seems to be happening and I want to know if I just have to correct for it with some kind of scale factor or if there is a fix or if I’m doing it all wrong

@Monkeyman32123 I don’t think anything should be stretched. Sounds like one of your draws has the x or y scale changed.

Hm, it’s only stretching on the X axis, I didn’t think it should be happening either. I’ll try to get the code up

displayMode(OVERLAY)
displayMode(FULLSCREEN_NO_BUTTONS)
supportedOrientations(LANDSCAPE_ANY)
function setup()
    --GRAYSCALER SETUP--
    w = WIDTH/2
    h = HEIGHT/2
    m=mesh() 
    m:addRect(w,h,WIDTH,HEIGHT)
    img =image(WIDTH,HEIGHT)
    m.texture=img
    --m.shader=shader(GrayScaleShader.v,GrayScaleShader.f)
    --m.shader.luminance=vec3(.3,.6,.1)
    --END GRAYSCALER SETUP--
    MENU=1;GAME=2
    state = MENU
    parameter.boolean("grayscale", true)
    scaler = WIDTH/HEIGHT
    p=PolarAnim()
end
function draw()
    if grayscale then setContext(img) end
    p:draw(true)
    sprite("Planet Cute:Character Boy",WIDTH/2,HEIGHT/2)
    if grayscale then setContext(); m:draw() end
end

PolarAnim = class()
function PolarAnim:init()
    --[[ example setup for polar grapher
    ]]--
    self.red = true 
    pi,sin,cos,tan,asin,acos,atan,log,ln,abs,sqrt = 
    math.pi,math.sin,math.cos,math.tan,math.asin,
    math.acos,math.atan,math.log,math.ln,math.abs,math.sqrt
    self.xmi,self.xma,self.rangex,self.ymi,self.yma,
    self.rangey,theta,self.blend = 0,0,0,0,0,0,0,0
    self.blendstep = .01
    self:grid(-200,200,-200,200)
    local thsteptbl = {77.70001,pi/3.3333,pi/4.4444,pi/11.111,pi/2.2222,pi/1.1111,pi/9.9999;
    pi/1.23456,pi/.7777,pi*3.3333,pi*1.1111,pi*4.4444,pi*.757575;
    pi*pi,pi*pi*pi*pi*pi+.000003,pi*.5001,thstep = pi*.1111,thstep = pi*.2222}
    self.thstep = thsteptbl[math.random(#thsteptbl)]
    g = math.random(200)/2
    self.func = "200*cos(g*theta)"
    self.img = image(math.min(WIDTH,HEIGHT),math.min(WIDTH,HEIGHT))
    self.perframe = math.random(10,100)
    self.drawcol = true
    self.frame=0
    self.a = true
end
function PolarAnim:draw(draw)
    if self.drawcol == true then
        self:colorCycle()
        else
        if self.a then
            self.frame = self.frame + 1
            if self.frame == 254 then self.a = false end
            else
            self.frame = self.frame - 1
            if self.frame == 0 then self.a = true end
        end
    end
    --background(255)
        for i=0,self.perframe do
            self:calcpoint()
        end
    if draw then
    pushMatrix()
        --[[
    if grayscale then
    translate(math.min(WIDTH,HEIGHT)/2-96,math.min(WIDTH,HEIGHT)/2)
    sprite(self.img,0,0,HEIGHT/scaler,HEIGHT)
    else
        --]]
         translate(math.min(WIDTH,HEIGHT)/2,math.min(WIDTH,HEIGHT)/2)
    sprite(self.img)  
    --end
    popMatrix()
    end
end
function PolarAnim:calcpoint()
    local dist = loadstring("return "..self.func)()
    local x = dist*math.cos(theta)
    local y = dist*math.sin(theta)
    local lastpoi = poi
    self:convertRawToScreenCoords(x,y)
    setContext(self.img)
    smooth()
    if self.drawcol == false then
        stroke(self.frame%255)
    elseif self.drawcol == true then
    stroke(self.col)
    end
    fill(stroke())
    strokeWidth(1)
        if lastpoi then
            line(lastpoi.x,lastpoi.y,poi.x,poi.y)
        end
    if grayscale then setContext(img) else setContext() end
    theta = theta + self.thstep
end
function PolarAnim:grid(xmin,xmax,ymin,ymax)
    self.origin = vec2(0,0)
    if xmin<0 and 0<xmax then
        self.xmi = xmin
        self.xma = xmax
        self.rangex = xmax - xmin
        local xaxisloc = 0-xmin
        self.origin.x = (xaxisloc/self.rangex)*math.min(WIDTH,HEIGHT)
        else
        self.xmi = xmin
        self.xma = xmax
        self.rangex = xmax - xmin
        local xaxisloc = 0-xmin
        self.origin.x = (xaxisloc/self.rangex)*math.min(WIDTH,HEIGHT)
    end
    if ymin<0 and 0<ymax then
        self.ymi = ymin
        self.yma = ymax
        self.rangey = ymax-ymin
        local yaxisloc = 0-ymin
        self.origin.y = (yaxisloc/self.rangey)*math.min(WIDTH,HEIGHT)
        else
        self.ymi = ymin
        self.yma = ymax
        self.rangey = ymax-ymin
        local yaxisloc = 0-ymin
        self.origin.y = (yaxisloc/self.rangey)*math.min(WIDTH,HEIGHT)
    end
end
function PolarAnim:convertRawToScreenCoords(x,y)
    local xp = x-self.xmi
    local coord = vec2((xp/self.rangex)*math.min(WIDTH,HEIGHT),0)
    local yp = y-self.ymi
    coord.y = (yp/self.rangey)*math.min(WIDTH,HEIGHT)
    drawpoint = true
    poi = coord
    return coord
end
function PolarAnim:colorCycle()
    self.blend = self.blend + self.blendstep
    if self.red then
        local c2 = color(255, 0, 0, 255)
        local c1 = color(255,127.5,0,255)
        self.col = c1:mix(c2,self.blend)
        if self.blend >= 1 then
            self.blend = 0
            self.red = false
            self.orange = true
        end
        elseif self.orange then
        local c2 = color(255,127.5,0,255)
        local c1 = color(255,255,0,255)
        self.col = c1:mix(c2,self.blend)
        if self.blend >= 1 then
            self.blend = 0
            self.orange = false
            self.yellow = true
        end
        elseif self.yellow then
        local c2 = color(255,255,0,255)
        local c1 = color(127.5,255,0,255)
        self.col = c1:mix(c2,self.blend)
        if self.blend >= 1 then
            self.blend = 0
            self.yellow = false
            self.yellowgreen = true
        end
        elseif self.yellowgreen then
        local c2 = color(127.5,255,0,255)
        local c1 = color(0,255,0,255)
        self.col = c1:mix(c2,self.blend)
        if self.blend >= 1 then
            self.blend = 0
            self.yellowgreen = false
            self.green = true
        end
        elseif self.green then
        local c2 = color(0,255,0,255)
        local c1 = color(0, 255, 127.5, 255)
        self.col = c1:mix(c2,self.blend)
        if self.blend >= 1 then
            self.blend = 0
            self.green = false
            self.seafoamgreen = true
        end
        self.blend = self.blend + .001
        elseif self.seafoamgreen then
        local c2 = color(0,255,127.5,255)
        local c1 = color(0, 255, 255, 255)
        self.col = c1:mix(c2,self.blend)
        if self.blend >= 1 then
            self.blend = 0
            self.seafoamgreen = false
            self.teal = true
        end
        elseif self.teal then
        local c2 = color(0,255,255,255)
        local c1 = color(0,127.5,255,255)
        self.col = c1:mix(c2,self.blend)
        if self.blend >= 1 then
            self.blend = 0
            self.teal = false
            self.skyblue = true
        end
        elseif self.skyblue then
        local c2 = color(0,127.5,255,255)
        local c1 = color(0,0,255,255)
        self.col = c1:mix(c2,self.blend)
        if self.blend >= 1 then
            self.blend = 0
            self.skyblue = false
            self.blue = true
        end
        elseif self.blue then
        local c2 = color(0,0,255,255)
        local c1 = color(127.5,0,255,255)
        self.col = c1:mix(c2,self.blend)
        if self.blend >= 1 then
            self.blend = 0
            self.blue = false
            self.purple = true
        end
        elseif self.purple then
        local c2 = color(127.5,0,255,255)
        local c1 = color(255, 0, 255, 255)
        self.col = c1:mix(c2,self.blend)
        if self.blend >= 1 then
            self.blend = 0
            self.pink = true
            self.purple = false
        end
        elseif self.pink then
        local c2 = color(255, 0, 255, 255)
        local c1 = color(255, 0, 127.5, 255)
        self.col = c1:mix(c2,self.blend)
        if self.blend >= 1 then
            self.blend = 0
            self.fuscia = true
            self.pink = false
        end
        elseif self.fuscia then
        local c2 = color(255,0,127.5,255)
        local c1 = color(255,0,0,255)
        self.col = c1:mix(c2,self.blend)
        if self.blend >= 1 then
            self.blend = 0
            self.red = true
            self.fuscia = false
        end
    end
end

Ignore the part with the shader, but whenever I run this the image sprited (in PolarAnim) is stretched along the x axis ( heavily) and the planet cute boy is fine

That code works fine for mr

Yeah, sorry, I forgot it was only the polar animation thing going wrong, not other sprites (been a while since I visited this problem), I changed the code now :stuck_out_tongue: it still isn’t working, I just originally posted the test that did work instead of the one that didn’t (by accident, I assure you)

Please try the code in my latest code-bearing comment ( once again I apologize @Ignatz, before I posted the wrong code, but I changed it to the correct code, now)

Look for this line

if grayscale then setContext(img) else setContext() end

And put this line before it

setContext() 

This seems to fix the problem, which is that you shouldn’t set context to a new image before closing the old context.

(I found it the hard way, commenting out lines and seeing which ones affected the drawing).

Huh, that’s really odd. I’m glad it wasn’t some super obvious error of my own though. That said, I give you infinite thanks @Ignatz :slight_smile: you’re a great help, and I’d have never found that myself (seriously, I spent days working ratios and obsessing over what was wrong…I think I grew a beard at one point in the work montage). You’re a life saver! gratuitous tip

Not a bug, no. Just remember to turn things off :wink:

Okay, Deleted the part of my comment that speculated about it being a bug :stuck_out_tongue: I’m not questioning your knowledge, but could you explain to me why I have to do setContext() then setContext(otherImg)? I don’t understand, I assume it’s something like how translations are additive, but I don’t get how I could be setting the context to both or messing with it somehow (I guess I just feel like setting the context to something else would close the other context and change it to the new one)

I would have thought setting a new context would close the previous one, but I guess the developers never expected someone to do two custom drawings one after the other.

I can’t tell you why not closing it messes things up.

Ah, well, thank you very much anyways :slight_smile: yeah, two custom draws probably isn’t the most common routine, is it? Well, since I’m very curious as to what causes that, I will attempt to summon the almighty @Simeon from his lair. Please explain it to me lord developer man (when/if you have the time, as I know you are very busy)

I didn’t want to start a whole new thread for this question, so I’m just putting it here: is there an (easy) way to draw a line in three dimensions? If not, what would be the hard way?

@Monkeyman32123 - you have to draw a 3D line, which means you start with a cube and stretch it along one dimension and shrink the other two dimensions.

The 3D demo included with Codea has code for creating a cube and it should be pretty easy to stretch that into a line. I wouldnt use the rest of that code as a guide to 3D though, because the camera settings are unusual.