determine centerpoint in irregular rectangle / create "composition"



  • Hi Iam still learning the basics and i have a new problem !

    I drew a rectangle using lineTo commands and curveTo commands
    and I managed to center it but I wanted to ask if there is a way to treat the 4 commands more like a group or rect without loosing the ability to control the handles.

    the only solution i found was to return a distance value for each point

    thanks a lot

    regards david

    for i in range(1) :
        newPage (5000,5000)
        xMiddle, yMiddle = 2500, 2500  # mitte 
        
        stroke(0)
        fill(0)
        
         
        
        
        
        
        translate(xMiddle, yMiddle)
       
    
        
        
            
        for i in range(1): #Body
            translate (-450,450)
            stroke()
            
                    # create a new empty path
            newPath()
            # set the first oncurve point
            moveTo((-0, -0))
            # line to from the previous point to a new point
            curveTo((74, -362), (-106, -360),(-0, -900))
            lineTo((900, -900))
            curveTo((162, -290), (450, -292),(900,-0))
            
            
    
            # curve to a point with two given handles
    
            # close the path
            closePath()
            # draw the path
            drawPath()
    
    


  • hello David,

    rect(), oval(), etc are regular Python functions. you can create a new shape that behaves the same way by defining a new ‘shape drawing function’ that takes the same arguments (x, y, w, h).

    here’s an example:

    def myShape(x, y, w, h):
        save()
        translate(x, y)
        scale(w, h)
        newPath()
        moveTo((0., 0.0))
        lineTo((1.0, 0.0))
        curveTo((0.7, 0.3), (0.7, 0.7), (1.0, 1.0))
        lineTo((0, 1.0))
        closePath()
        drawPath()
        restore()
    
    newPage(500, 500)
    
    xMiddle = width()/2
    yMiddle = height()/2
    
    shapeWidth, shapeHeight = 354, 242
    
    shapeX = xMiddle - shapeWidth/2
    shapeY = yMiddle - shapeHeight/2
    
    myShape(shapeX, shapeY, shapeWidth, shapeHeight)
    

    notice that the (x,y) point positions are now defined as decimal numbers between 0.0 and 1.0, which are then scaled by the given width & height.



  • @gferreira said in determine centerpoint in irregular rectangle / create "composition":

    def myShape(x, y, w, h):
    save()
    translate(x, y)
    scale(w, h)
    newPath()
    moveTo((0., 0.0))
    lineTo((1.0, 0.0))
    curveTo((0.7, 0.3), (0.7, 0.7), (1.0, 1.0))
    lineTo((0, 1.0))
    closePath()
    drawPath()
    restore()

    newPage(500, 500)

    xMiddle = width()/2
    yMiddle = height()/2

    shapeWidth, shapeHeight = 354, 242

    shapeX = xMiddle - shapeWidth/2
    shapeY = yMiddle - shapeHeight/2

    myShape(shapeX, shapeY, shapeWidth, shapeHeight)

    Thanks Mister Ferreira
    for your quick and detailed answer

    can I ask you why suddenly everything is now in decimal numbers ?
    this is the only thing i didnt really understand

    Best regards David



  • can I ask you why suddenly everything is now in decimal numbers ?

    think of it as percentages: the x/y point coordinates are defined in relation to the width/height dimensions of the shape, which are variable.

    ps. width/height were used as scale factors – not the best implementation in hindsight, because it also scales the stroke width. this (below) is better, and maybe makes the percentages explanation clearer:

    def myShape(x, y, w, h):
        save()
        translate(x, y)
        newPath()
        moveTo((0.0*w, 0.0*h))
        lineTo((1.0*w, 0.0*h))
        curveTo((0.7*w, 0.3*h), (0.7*w, 0.7*h), (1.0*w, 1.0*h))
        lineTo((0*w, 1.0*h))
        closePath()
        drawPath()
        restore()
    

    cheers!



  • thanks

    @}->--


Log in to reply