Polygon set_width and set_height not work - manim

There is right part inverse proportional function (1/x) in the scene, in the begining I draw a rectangle(using polygon), the left down corner is (0, 0, 0), thei right up corner is on the function with x=3.
I override update_mobjects, and update the postion of the right upper corner of rectangle with some point on the function, but not work, the width increase instead of decrease, please help, thanks!
my code is as below:
class Chess(GraphScene):
CONFIG = {
"x_min": -5,
"x_max": 5,
"y_min": -5,
"y_max": 6,
"y_axis_height": 10,
"graph_origin": ORIGIN,
"x_labeled_nums": range(-5, 5, 1),
"y_labeled_nums": range(-5, 6, 1)
}
def construct(self):
self.setup_axes(animate=True)
if1r = self.get_graph(lambda x: 1 / x, color=YELLOW_D, x_min=1 / self.y_max, x_max=self.x_max)
self.play(FadeIn(if1r))
spt1 = self.input_to_graph_point(3, if1r)
sg = Polygon(np.array([0, 0, 0]), np.array([spt1[0], 0, 0]), spt1, np.array([0, spt1[1], 0]), fill_color=RED_D,
fill_opacity=1.0)
self.add(sg)
self.sg = sg
self.sva1 = 3
self.if1r = if1r
self.moving3()
def moving3(self):
self.always_update_mobjects = True
self.wait(10)
def update_mobjects(self, dt):
if (hasattr(self, "sva1") and self.sva1 > 1):
self.sva1 = self.sva1 - 0.01
spt2 = self.input_to_graph_point(self.sva1, self.if1r)
self.sg.set
self.sg.set_width(spt2[0], stretch=False, about_point=self.sg.get_corner(DOWN + LEFT))
self.sg.set_height(spt2[1], stretch=False, about_point=self.sg.get_corner(DOWN+LEFT))

Yes it works, but you forgot to change stretch=True in the last two lines.
In addition, the line self.sg.set left over.
If you allow me the comment, I think you have better control in this way:
More examples here
class NewChess(GraphScene):
CONFIG = {
"x_min": -5,
"x_max": 5,
"y_min": -5,
"y_max": 6,
"y_axis_height": 10,
"graph_origin": ORIGIN,
"x_labeled_nums": range(-5, 5, 1),
"y_labeled_nums": range(-5, 6, 1),
"x_start":3,
"x_end":1
}
def get_rectangle_from_point(self,point):
return Polygon(
[0, 0, 0], #(0,0)
[point[0], 0, 0], #(x,0)
point, #(x,y)
[0, point[1], 0], #(0,y)
fill_color=RED_D,
fill_opacity=1.0
)
def construct(self):
self.setup_axes()
x_coord_tracker=ValueTracker(self.x_start)
if1r = self.get_graph(lambda x: 1 / x, color=YELLOW_D, x_min=1 / self.y_max, x_max=self.x_max)
spt1 = self.input_to_graph_point(x_coord_tracker.get_value(), if1r)
sg = self.get_rectangle_from_point(spt1)
self.play(FadeIn(if1r))
self.play(FadeIn(sg))
def update_rectangle(rectangle):
new_coord=self.input_to_graph_point(x_coord_tracker.get_value(), if1r)
rectangle.become(self.get_rectangle_from_point(new_coord))
return rectangle
sg.add_updater(update_rectangle)
self.add(sg) # Add object again
self.play(
x_coord_tracker.set_value,self.x_end,
rate_func=linear,
run_time=2
)
self.wait()

Related

There's a good method to create a tilemap from an ascii matrix?

The idea is simple, but the execution is bothering me.
I've created a small random dungeon generator that create a grid like this:
000001
000111
000111
001101
011101
011111
This is a sample 6x6 dungeon where 0 is a wall and 1 is an open path.
The conversion from this to some sort of tile id map is simple, and trivial, but creating the image itself is the hard part.
I want to know if there's a lib, or method to achieve that. If not, then what would you do?
This is not part of a game, and only a dungeon generator for DND. Any language is OK, but the generator was made in Go.
You can use OpenCV for this task. Probably PIL can do the same, don't have exp with it.
import cv2
import numpy as np
data_list = [
[0, 0, 0, 0, 0, 1],
[0, 0, 0, 1, 1, 1],
[0, 0, 0, 1, 1, 1],
[0, 0, 1, 1, 0, 1],
[0, 1, 1, 1, 0, 1],
[0, 1, 1, 1, 1, 1]
]
arr = np.array(data_list, dtype=np.uint8) * 255
arr = cv2.resize(arr, (0, 0), fx=50, fy=50, interpolation=cv2.INTER_NEAREST)
cv2.imshow("img", arr)
cv2.waitKey()
# or you can save on disk
cv2.imwrite("img.png", arr)
use np.block()
# a bunch of sprites/images, all the same size
# load them however you like
tiles = [...]
data_list = [
[0, 0, 0, 0, 0, 1],
[0, 0, 0, 1, 1, 1],
[0, 0, 0, 1, 1, 1],
[0, 0, 1, 1, 0, 1],
[0, 1, 1, 1, 0, 1],
[0, 1, 1, 1, 1, 1]
]
picture = np.block([
[tiles[k] for k in row]
for row in data_list
])
Or, if you use any kind of game engine, or something even more trivial, like SDL/PyGame, simply "blit" each tile.
PIL, as you found out, is perfectly capable of blitting one image (tile) onto another (whole map).
I kind of managed to get a solution, but it will be a Python only.
Using PIL I can make a mosaic with tile images and create the map. It's not a solid solution made from scratch but it can do the Job.
I'm still open for another approach.
My solution is this method here:
matrix = np.loadtxt(input_file, usecols=range(matrix_square), dtype=int)
tiles = []
for file in glob.glob("./tiles/*"):
im = Image.open(file)
tiles.append(im)
output = Image.new('RGB', (image_width,image_height))
for i in range(matrix_width):
for j in range(matrix_height):
x,y = i*tile_size,j*tile_size
index = matrix[j][i]
output.paste(tiles[index],(x,y))
output.save(output_file)
The matrix_square is the matrix dimensions (as a square). I'm still working on a better solution, but this is working fine for me.
You need to change the tile_size to match the tile resolution that you're using.
This is a generated dungeon with this method
The tiles are bad, but the grid is fine enough.

keyboard keyisdown is not activating

I am trying to make my paddle move up and down when I press the d and a buttons.
When i press the d and a buttons nothing happens at all... they just sit there.....
anyway the code is below if you know please tell me. thanks
I am aware the indenting is wrong i had to change if for this post:)
window_width = 580
window_height = 420
playerScore = 0
enemyScore = 0
pSpeed = 200
player1Y = 100
player2Y = -20
function love.load()
love.window.setMode(window_width, window_height)
end
function love.draw()
love.graphics.printf('hello pong', 0, window_height / 2 - 6, window_width, 'center')
love.graphics.rectangle("fill", 500, player1Y , 10, 50)
love.graphics.rectangle("fill", 40, 220, 10, 50)
love.graphics.rectangle("fill", 100, 230, 7, 5)
end
function update(dt)
if love.keyboard.isDown('d') then
player1Y = player1Y + pSpeed * dt
elseif love.keyboard.isDown('a') then
player1Y = player1Y + -pSpeed * dt
end
end
Try pSpeed = 10 instead of 200, so your rectangles aren't jumping beyond window_height in 2 steps.
And change colors for each rectangle, so it's easier to see what's happening.
function love.draw()
love.graphics.setColor( 0, 0, 0 ) -- black
love.graphics.printf( 'hello pong', 0, window_height /2 -6, window_width, 'center' )
love.graphics.setColor( 1, 0, 0 ) -- red
love.graphics.rectangle( "fill", 500, player1Y, 10, 50 )
love.graphics.setColor( 0, 1, 0 ) -- green
love.graphics.rectangle( "fill", 40, 220, 10, 50 )
love.graphics.setColor( 0, 0, 1 ) -- blue
love.graphics.rectangle( "fill", 100, 230, 7, 5 )
end
I think you should write "love.update(dt)" instead of writing only "update(dt)". I tried changing that and it worked! Also, indenting does not affect the way your code works in Lua, but it does massively improve readability.

(gLua) Derma not working. attempt to call method 'Close' (a nil value)

Error at function ContextClose()
And all derma menu not working
attempt to call method 'Close' (a nil value)
I've create derma menu and it worked in garrysmod\lua folder,
then I moved flies to addon floder and it stopped working.
cl_init.lua
AddCSLuaFile("pbfhud.lua")
include("pbfhud.lua")
pbfhud.lua
AddCSLuaFile()
local statBarW, statBarH = ScrW() / 400, ScrH() / 10
local barW, barH = ScrW() / 4, ScrH() / 10
local btnW, btnH = ScrW() / 4, ScrH() / 20
surface.CreateFont( "NotDermaDefault", {
font = "Arial",
extended = false,
size = 25,
weight = 500,
blursize = 0,
scanlines = 0,
antialias = true,
underline = false,
italic = false,
strikeout = false,
symbol = false,
rotary = false,
shadow = false,
additive = false,
outline = false,
} )
local function DrawInventorySlot( )
end
local function DrawBar(x, y, w, h, color, text, val, name)
local rtext = text .. ": " .. val
local tW, tH = surface.GetTextSize(rtext)
tH = tH / 2
if (name == true) then
tW = tW / 7
end
draw.RoundedBox(0, x, y, w, h, color)
draw.SimpleText(rtext, "NotDermaDefault", barW / 2 - tW / 7, barH / 2 - tH + y, Color( 255, 255, 255, 255 ), TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP)
end
function ContextOpen()
gui.EnableScreenClicker( true )
menu = vgui.Create("DFrame")
menu:SetSize(ScrW(), ScrH() + 100)
menu:ShowCloseButton(false)
menu:SetTitle("")
menu:Center()
menu:SetDraggable(false)
menu:SetBackgroundBlur(true)
menu:SetMouseInputEnabled(true)
menu.Paint = function()
draw.RoundedBox(0, 0, 0, ScrW(), ScrH(), Color(0,0,0,100))
local blur = Material("pp/blurscreen")
local function blurPanel(panel, amount)
local x, y = panel:LocalToScreen(0, 0)
local scrW, scrH = ScrW(), ScrH()
surface.SetDrawColor(255, 255, 255)
surface.SetMaterial(blur)
for i = 1, 6 do
blur:SetFloat("$blur", (i / 3) * (amount or 6))
blur:Recompute()
render.UpdateScreenEffectTexture()
surface.DrawTexturedRect(x * -1, y * -1, scrW, scrH)
end
end
blurPanel(menu, 5)
draw.RoundedBox(0, 0, 0, ScrW() / 4, ScrH(), Color(0,0,0,50))
-- Stats
DrawBar(0, 0, barW, barH, Color(235, 235, 235, 140), "Имя", LocalPlayer():getDarkRPVar("rpname"), true )
DrawBar(0, statBarH, LocalPlayer():Health() * statBarW, barH, Color(255, 50, 43, 140), "Здоровье", LocalPlayer():Health(), false )
DrawBar(0, statBarH * 2, LocalPlayer():Armor() * statBarW, barH, Color(43, 128, 255, 140), "Броня", LocalPlayer():Armor(), false )
DrawBar(0, statBarH * 3, barW, barH, Color(255, 149, 43, 140), "Работа", LocalPlayer():getDarkRPVar("job"), false )
DrawBar(0, statBarH * 4, barW, barH, Color(0, 138, 11, 140), "Зарплата", LocalPlayer():getDarkRPVar("salary"), false )
-- Inventory
draw.RoundedBox(0, barW, 0, ScrW() / 1.7, ScrH(), Color(0,0,0,50))
end
-- Buttons
--fb = vgui.Create("DButton", menu)
--fb:SetSize(btnW, btnH)
--fb:SetPos(0, btnH * 12)
--fb:SetText("")
end
function ContextClose()
menu:Close()
gui.EnableScreenClicker( false )
end
hook.Add ("OnContextMenuOpen", "Context", ContextOpen)
hook.Add ("OnContextMenuClose", "Context", ContextClose)
Files location
..addons\pbf_hud\lua\autorun\client\cl_init.lua
..addons\pbf_hud\lua\autorun\client\pbfhud.lua
SOLVED
I used same identifiers for hooks. If you have same problem just change hook indentifier:
From:
hook.Add ("OnContextMenuOpen", "Context", ContextOpen)
hook.Add ("OnContextMenuClose", "Context", ContextClose)
To:
hook.Add ("OnContextMenuOpen", "Context_open", ContextOpen)
hook.Add ("OnContextMenuClose", "Context_close", ContextClose)

Holt winters Alpha

I am trying to get the HoltWinters Alpha function added.
I have a Table called Sales1 and the code should refer to this table.
Is there anyone who can correct or amend my code below ,so i get the Holtwinters Alpha instead of the Chronbachs Alpha?
Holt winters calc (need this probably amended)
library(forecast)
library(Metrics
)
read_file(sales1)
x <- sales
x = c(Sales1)
mSES = HoltWinters(x, alpha = 0.5, beta = FALSE, gamma = FALSE)
mHW = HoltWinters(x, alpha = 0.5, beta = FALSE, gamma = FALSE)
mSES$SSE
mHW$SSE
HoltWinters(x, alpha = NULL, beta = NULL, gamma = NULL,
seasonal = c("additive", "multiplicative"),
start.periods = 2, l.start = NULL, b.start = NULL,
s.start = NULL,
optim.start = c(alpha = 0.3, beta = 0.1, gamma = 0.1),
optim.control = list())
chronbachs alpha calc
read_file(sales1)
library(tidyverse)
library(psy)
Number of rows before to take into account
rolling = 2
sales1 <- sales::sales( ~date, ~sales,)
#Lag
sales1 = sales1 %>% mutate(lagsales = lag(sales))
#Rolling Chronbachs Alpha.:( I need the Holtwinter Alpha here )
sales1$alpha = c( rep(NA, rolling),
map_dbl((rolling + 1):nrow(sales1), function(x){
cronbach(sales1 %>% select(sales, lagsales) %>% slice((x-rolling):x))$alpha
})
)
sales1
tibbet from Sales1 table:
df <- tibble::tribble(
~seq, ~date, ~sales,
1, "3/01/2017", 40,
2, "4/01/2017", 2,
3, "5/01/2017", 2,
4, "6/01/2017", 2,
5, "7/01/2017", 30,
6, "8/01/2017", 2,
7, "1/02/2017", 9,
8, "2/02/2017", 5,
9, "3/02/2017", 65,
10, "4/02/2017", 3,
11, "5/02/2017", 65

How to repeat an image like css in lua?

https://gyazo.com/d0d0bab65c0a7060972988a5e73c7959
That was achieved by the this:
local x = script.Parent.Smile
local y = script.Parent.Smile2
while true do
x:TweenPosition(UDim2.new(0, 0, 1, 0))
y:TweenPosition(UDim2.new(0, 0, 1, 0))
wait(.1)
x.Position = y.Position + UDim2.new(0, 0, -1, 0)
y.Position = UDim2.new(0, 0, 0, 0)
end
I was wondering if there was a better way to do it and make it smoother(slower)?
If you want it to look as if it endlessly descends, try adding a "Linear" parameter to your tweens. It will keep the tween constant in speed. For example:
local x = script.Parent.Smile
local y = script.Parent.Smile2
while true do
x:TweenPosition(UDim2.new(0, 0, 1, 0),"Out","Linear",0.1)
y:TweenPosition(UDim2.new(0, 0, 1, 0),"Out","Linear",0.1)
wait(.105) -- making sure to tween again after 0.1 seconds, the 0.105 safely accounts for any latency in the game
x.Position = y.Position + UDim2.new(0, 0, -1, 0)
y.Position = UDim2.new(0, 0, 0, 0)
end
Try that script above, if it doesn't work you can blame me for it

Resources